Online
 
Thursday, 20 November 2008
 
 

Website PHP - Variables of PHP | Print |  E-Mail
 

PHP - PHP Language Programing 

PHP - Variables of PHP things that store databegin with $ followed by a letter or an underscore, then any combination of letters, numbers, and the underscore character. This means you may not start a variable with a number. One notable exception to the general naming scheme for variables are "variable variables,".

Valid and invalid variable names

$myvar

Correct

$Name

Correct

$_Age

Correct

$_ __AGE_ __

Correct

$91

Incorrect ; starts with a number

$1Name

Incorrect ; starts with a number

$Name91

Correct; numbers are fine at the end and after the first character

$_Name91

Correct

$Name's

Incorrect; no symbols other than "_" are allowed, so apostrophes are bad


Variables are case-sensitive, which means that $Foo is not the same variable as $foo, $FOO, or $fOO.

Assigning variables is as simple as using the assignment operator (=) on a variable, followed by the value you want to assign. Here is a basic script showing assigning and outputting datanote the semicolons used to end each statement:

 <?php

$name = "Paul";

print "Your name is $name\n";

$name2 = $name;

$age = 20;

print "Your name is $name2, and your age is $age\n";

print 'Goodbye, $name!\n';

?>

There we set the $name variable to be the string Paul, and PHP lets us print out that variable after Your name is. Therefore, the output of the first print statement is Your name is Paul, because PHP will substitute $name for its value whenever it finds it by itself, or inside a double-quoted string (that is, one starting and ending with").

We then set $name2 to be $name, which effectively copies $name's value into $name2. $name2 is now set to Paul. We also set up the $age variable to be the number 20. Our second print statement outputs both variables at once, as again, PHP will substitute them inside the string.

However, the last print statement will not replace $name with Paul. Instead, it will print:

 Goodbye, $name!\n


The reason for this is that PHP will not perform variable substitution inside single-quoted strings, and won't even replace most escape characters (the exception being \'). In double-quoted strings, PHP will replace $name with its value; in a single-quoted string, PHP will consider $name to mean that you actually want it to output the text $name just like that.

When you want to append something to your variable while inside a string, PHP may consider the characters to be part of the variable. For example:

 <?php

$food = "grapefruit";

print "These $foods aren't ripe yet.";

?>

While the desired output was These grapefruits aren't ripe yet, the actual output is different: because we have added the "s" to the end of the variable name, we have changed it from trying to read $food to trying to read $foods. The variable $foods does not exist, so PHP will leave the space blank and may generate an error. There are two ways to solve this:

 <?php

$food = "grapefruit";

print "These ${food}s aren't ripe yet.";

print "These {$food}s aren't ripe yet.";

?>

The braces, { and }, technically signal a variable variable when used inside a string, but in the example above, they are used to tell PHP where the variable ends. You don't need to use braces where characters being appended to a variable would make the variable name illegal, like this:

 <?php

$food = "grapefruit";

print "This $food's flavour is bad.";

?>

That will work because you are not allowed to use apostrophes as part of your variable names.

Read More PHP:PHP - Functions | PHP - Including Other Files | PHP - Comments Tutorial | PHP - Example Whitespace | PHP - Variables of PHP | PHP - Basics of PHP | PHP Abnormal Script | PHP - Output Control

Links to this article:"PHP - Variables of PHP"

 

This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Jumbo Coklat
 
Top! Top!