FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Is it safe to edit arrays?





garionw
Today I just discovered the power of Arrays, here's what I have:

Code:
$_SESSION['errors'] = array('key' => true, 'username' => false, 'passwords' => false);
 $_SESSION['values'] = array('key' => '', 'username' => '', 'passwords' => '');


Obliviously its for error's in a form.

What I'd like to know is, how can I edit the value of one of the values? I currently have $_SESSION['values']['key'] = 'garion';, but I was reading this on the array page:

Quote:
Just a helpful note, when creating arrays, avoid doing:

<?php
$var[key] = "value";
?>

PHP will look for a constant called 'key' before it will treat it as a string, thus slowing down execution (I've seen files with thousands of these and PHP taking over a second to execute).


Could someone tell me what this means please? I am under the impression that doing what I am doing $_SESSION['values']['key'] = 'garion'; will cause the script to slow down. Is this true, and how can I get around it?

Thanks for your time,
Garion
MrBlueSky
You can safely use

Code:

$_SESSION['values']['key'] = 'garion';



The quote from php.net talks about

Code:

$var[key] = "value";


which specifies the key without quotes around it. When you don't use the quotes and a constant named key is defined it will not work. For example, consider this code:

Code:

<?php

  define("foo", "bar");
  $arr["foo"] = "Good";
  $arr["bar"] = "Bad";
  echo $arr[foo]."<BR>\n";
  echo $arr["foo"];

?>


This will output:

Quote:

Bad
Good


So when you use quotes (as you do in $_SESSION['values']['key'] = 'garion') you are always safe.
garionw
MrBlueSky wrote:
You can safely use

Code:

$_SESSION['values']['key'] = 'garion';



The quote from php.net talks about

Code:

$var[key] = "value";


which specifies the key without quotes around it. When you don't use the quotes and a constant named key is defined it will not work. For example, consider this code:


Thanks for that, does that mean that I should always use the ' ' in things like variables and functions (both inbuilt and custom build) and save the " " for echoing text?
MrBlueSky
garionw wrote:

Thanks for that, does that mean that I should always use the ' ' in things like variables and functions (both inbuilt and custom build) and save the " " for echoing text?


Thanks for the $Fri Smile

But you can use both single quotes and double quotes everywhere:

Code:

// No problem:
$_SESSION["values"]["key"] = 'garion';
echo 'Hello ';
echo "world!\n";
$somearray['key1'] = $anotherarray["key2"];

// This is not a good idea:
$somearray[key1] = $anotherarray[key2];
Agent ME
The difference between ' and " is...
Code:
<?php
$lol = 34;
echo "$lol";
echo '$lol';
?>

will output
Quote:
34
$lol


' is literal - and " is parsed for variable names.
So echo "The value of lol is $lol" will have $lol replaced with its value.
MrBlueSky
Agent ME wrote:
The difference between ' and " is...
Code:
<?php
$lol = 34;
echo "$lol";
echo '$lol';
?>

will output
Quote:
34
$lol


' is literal - and " is parsed for variable names.
So echo "The value of lol is $lol" will have $lol replaced with its value.


And of course some escaped sequences (\n for example)

See: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Related topics
Strange Fire fox problem
The Archive of All Funny
A Guide to Safe FAX
How To : Improve Your PHP Programming
safe mode
Chatroom Script
HOW TO ENSURE THAT VIRUS DOES NOT SPREAD THRU YOUR ADDRESSBk
Marketplace Blacklist! - Read to avoid being scammed!
Trippling Your Download Speeds
Visual Basic Tutorial - Control Arrays: What, Why and How
C/C++
A confusion with C# and VB.NET...
Toshiba 4S Reactors, Super Safe Small Simple
Generic arrays in Java
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.