473,382 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Array

I have made a script that redirect a user to the page related to the month.
I bind the month numbers to the url as uitrukken.php?md=05

Now....i would print the month name on my page. I have made the following
array.

<?PHP
$maanden = array(
01 => "Januari",
02 => "Februari",
03 => "Maart",
04 => "April",
05 => "Mei",
06 => "Juni",
07 => "Juli",
08 => "Augustus",
09 => "September",
10 => "Oktober",
11 => "November",
12 => "December"
);

echo $maanden['$md'];
?>

Why doen't show dis the month. In which format can i use a array with a
variabele as echo $maanden['$md'];

FiremanSAM
Jul 17 '05 #1
9 3359
In article <tZfbb.5640$P51.8734@amstwist00>,
"FiremanSAM" <fi********@co.uk> wrote:
Why doen't show dis the month. In which format can i use a array with a
variabele as echo $maanden['$md'];


use double quotes, otherwise variables will not be interpolated:

echo $maanden["$md"];

If register_globals is off in your setup, you'll need to do this:

echo $maanden[$_GET['md']];

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #2
"FiremanSAM" <fi********@co.uk> schreef in bericht
news:tZfbb.5640$P51.8734@amstwist00...
I have made a script that redirect a user to the page related to the month. I bind the month numbers to the url as uitrukken.php?md=05

Now....i would print the month name on my page. I have made the following
array.

<?PHP
$maanden = array(
01 => "Januari",
02 => "Februari",
03 => "Maart",
04 => "April",
05 => "Mei",
06 => "Juni",
07 => "Juli",
08 => "Augustus",
09 => "September",
10 => "Oktober",
11 => "November",
12 => "December"
);

echo $maanden['$md'];
?>

Why doen't show dis the month. In which format can i use a array with a
variabele as echo $maanden['$md'];


Drop the single quotes:

echo $maanden[$md];
Jul 17 '05 #3
> > $maanden = array(
01 => "Januari",
02 => "Februari",
03 => "Maart",
04 => "April",
05 => "Mei",
06 => "Juni",
07 => "Juli",
08 => "Augustus",
09 => "September",
10 => "Oktober",
11 => "November",
12 => "December"
);


you could of course use strftime("%B") (http://at2.php.net/strftime) and
setlocale() (http://at2.php.net/manual/en/function.setlocale.php) to have
the month names in your language.....
Jul 17 '05 #4
Jan Pieter Kunst <de*****@cauce.org> wrote in message >
use double quotes, otherwise variables will not be interpolated:

echo $maanden["$md"];


sorry, you've just hit my pet-peeve. Surrounding a variable in
double-quotes is a waste, PHP doesn't know what's in the quotes and
must go in a parse the string to find any variables to expand,
escaped, chars, or whatever. Variables don't need to be quoted.. just
use $maanden[$md]. Can someone back-up my belief that they
"shouldn't" be quoted?
Jul 17 '05 #5
Br******@swbell.net (BKDotCom) schrieb:
Jan Pieter Kunst <de*****@cauce.org> wrote in message >
use double quotes, otherwise variables will not be interpolated:

echo $maanden["$md"];


sorry, you've just hit my pet-peeve. Surrounding a variable in
double-quotes is a waste, PHP doesn't know what's in the quotes and
must go in a parse the string to find any variables to expand,
escaped, chars, or whatever. Variables don't need to be quoted.. just
use $maanden[$md]. Can someone back-up my belief that they
"shouldn't" be quoted?


It takes just some few extra microseconds of time if you use double
quotes, but that adds up with each time you run those scripts.

I prefer to use no quotes if there would be just one variable inside the
string and I use single quotes if the string contains no variable that
has to be interpolated.

Regards,
Matthias
Jul 17 '05 #6
In article <e8**************************@posting.google.com >,
Br******@swbell.net (BKDotCom) wrote:
use double quotes, otherwise variables will not be interpolated:

echo $maanden["$md"];


sorry, you've just hit my pet-peeve. Surrounding a variable in
double-quotes is a waste, PHP doesn't know what's in the quotes and
must go in a parse the string to find any variables to expand,
escaped, chars, or whatever. Variables don't need to be quoted.. just
use $maanden[$md]. Can someone back-up my belief that they
"shouldn't" be quoted?


You are right. I probably had the rule 'You should always use quotes
around a string literal array index'
(see <http://nl2.php.net/types.array>) in mind, but that is about string
literals, not about variables. There is no need to use double quotes
around variable array keys. (I doubt that there will be a noticeable
speed difference, though.)

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #7

"BKDotCom" <Br******@swbell.net> wrote in message
news:e8**************************@posting.google.c om...
Jan Pieter Kunst <de*****@cauce.org> wrote in message >
use double quotes, otherwise variables will not be interpolated:

echo $maanden["$md"];


sorry, you've just hit my pet-peeve. Surrounding a variable in
double-quotes is a waste, PHP doesn't know what's in the quotes and
must go in a parse the string to find any variables to expand,
escaped, chars, or whatever. Variables don't need to be quoted.. just
use $maanden[$md]. Can someone back-up my belief that they
"shouldn't" be quoted?


I *would* have agreed until I tested both... and you'll be surprised of the
results...

The performance difference would be small on a quiet server with a small
workload - However, given a busier environment, the performance enhancement
differs almost 50%.

In my test, I had a loop which checked the value of an array element - The
element was also a variable - First test had the element variable inside
quotes (thus $testArray["$element"]) while the second test excluded the
double quotes (thus $testArray[$element]).

I ran this on my server whereby I was the only user - the box was almost
asleep so I cannot blame it on other environment changes - I also ran the
test twice. Test 1 took 86seconds while test 2 took 152 seconds...

Surprised? So was I... It looks like its better to include double quotes as
opposed to excluding them - I've not worked out why just yet... I'll pitch a
seperate post asking folk to comment on this... My test script follows
below:

<?
// Perform a test $maxLoop number of times, count the number of seconds
// it takes to complete the loop - Test 1 has the array element named
// inside double quotes, Test 2 excludes the double quotes
set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 1 ////////////////////
$start=time();
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray["$elementName"]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 1: $difference<hr>");

set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 2 ////////////////////
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray[$elementName]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 2: $difference<hr>");
?>
Jul 17 '05 #8
*cough cough*
Your test 2 took 66 seconds.
you didn't reinitialize $start after test1 compleated... therefore
152 seconds is the total time your script ran.

so:
86 sec for test 1 (quotes)
66 sec for test 2 (no quotes)
152 sec: total

but, bonus points to you for actually taking the time to research this.
"Randell D." <yo**************************@yahoo.com> wrote in message news:<Ee*********************@news1.calgary.shaw.c a>...
"BKDotCom" <Br******@swbell.net> wrote in message
news:e8**************************@posting.google.c om...
Jan Pieter Kunst <de*****@cauce.org> wrote in message >
use double quotes, otherwise variables will not be interpolated:

echo $maanden["$md"];


sorry, you've just hit my pet-peeve. Surrounding a variable in
double-quotes is a waste, PHP doesn't know what's in the quotes and
must go in a parse the string to find any variables to expand,
escaped, chars, or whatever. Variables don't need to be quoted.. just
use $maanden[$md]. Can someone back-up my belief that they
"shouldn't" be quoted?


I *would* have agreed until I tested both... and you'll be surprised of the
results...

The performance difference would be small on a quiet server with a small
workload - However, given a busier environment, the performance enhancement
differs almost 50%.

In my test, I had a loop which checked the value of an array element - The
element was also a variable - First test had the element variable inside
quotes (thus $testArray["$element"]) while the second test excluded the
double quotes (thus $testArray[$element]).

I ran this on my server whereby I was the only user - the box was almost
asleep so I cannot blame it on other environment changes - I also ran the
test twice. Test 1 took 86seconds while test 2 took 152 seconds...

Surprised? So was I... It looks like its better to include double quotes as
opposed to excluding them - I've not worked out why just yet... I'll pitch a
seperate post asking folk to comment on this... My test script follows
below:

<?
// Perform a test $maxLoop number of times, count the number of seconds
// it takes to complete the loop - Test 1 has the array element named
// inside double quotes, Test 2 excludes the double quotes
set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 1 ////////////////////
$start=time();
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray["$elementName"]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 1: $difference<hr>");

set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 2 ////////////////////
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray[$elementName]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 2: $difference<hr>");
?>

Jul 17 '05 #9

"BKDotCom" <Br******@swbell.net> wrote in message
news:e8**************************@posting.google.c om...
*cough cough*
Your test 2 took 66 seconds.
you didn't reinitialize $start after test1 compleated... therefore
152 seconds is the total time your script ran.

so:
86 sec for test 1 (quotes)
66 sec for test 2 (no quotes)
152 sec: total

but, bonus points to you for actually taking the time to research this.
"Randell D." <yo**************************@yahoo.com> wrote in message

news:<Ee*********************@news1.calgary.shaw.c a>...
"BKDotCom" <Br******@swbell.net> wrote in message
news:e8**************************@posting.google.c om...
Jan Pieter Kunst <de*****@cauce.org> wrote in message >
> use double quotes, otherwise variables will not be interpolated:
>
> echo $maanden["$md"];

sorry, you've just hit my pet-peeve. Surrounding a variable in
double-quotes is a waste, PHP doesn't know what's in the quotes and
must go in a parse the string to find any variables to expand,
escaped, chars, or whatever. Variables don't need to be quoted.. just
use $maanden[$md]. Can someone back-up my belief that they
"shouldn't" be quoted?


I *would* have agreed until I tested both... and you'll be surprised of the results...

The performance difference would be small on a quiet server with a small
workload - However, given a busier environment, the performance enhancement differs almost 50%.

In my test, I had a loop which checked the value of an array element - The element was also a variable - First test had the element variable inside
quotes (thus $testArray["$element"]) while the second test excluded the
double quotes (thus $testArray[$element]).

I ran this on my server whereby I was the only user - the box was almost
asleep so I cannot blame it on other environment changes - I also ran the test twice. Test 1 took 86seconds while test 2 took 152 seconds...

Surprised? So was I... It looks like its better to include double quotes as opposed to excluding them - I've not worked out why just yet... I'll pitch a seperate post asking folk to comment on this... My test script follows
below:

<?
// Perform a test $maxLoop number of times, count the number of seconds
// it takes to complete the loop - Test 1 has the array element named
// inside double quotes, Test 2 excludes the double quotes
set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 1 ////////////////////
$start=time();
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray["$elementName"]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 1: $difference<hr>");

set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 2 ////////////////////
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray[$elementName]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 2: $difference<hr>");
?>

That is a bit embarrassing isn't it.... thanks for pointing that one out -
It could have steered me (and others) in the wrong direction...
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.