Variables in PHP
A variable holds data. This data might be a number, a string (i.e. text), or something more complex (like an array or object, which is beyond the scope of this tutorial). Variables are always written with a dollar sign at the beginning. To keep things easy, I recommend using only letters (case sensitive!) and maybe numbers in your variable names.
To create a variable, just assign it a value. For string values, you must enclose the value in 'apostrophes' or "quotes". Note that every line ends with a ; (semicolon).
Good examples of variables, illustrating the string, integer, and boolean data types:
<?php
$myVar1 = 'Hello!'; // The word Hello! saved as a string
$myVar2 = '1'; // the number 1 saved as a string (note the apostrophes)
$myVar3 = 1; // the number 1 saved as an integer (note the lack of apostrophes)
$myVar4 = 'true'; // the word true saved as a string
$myVar5 = true; // the boolean (logical) value of true. Frequently used booleans are true, false, and null.
?>Bad examples that will cause a “parse error” in PHP:
<?php
$myVar = 'Hello; // No closing apostrophe
$myVar = "Hello'; // If you start with " you must end with "
$myVar = "Hello" // No ending semicolon
$myVar = 'How's it going?'; // see explanation below
?>The last example really throws people off. PHP sees that the string starts with ', so when it sees the ' after How, it thinks that’s the end of the string, so it can’t figure out what to do with s it going. The result: A fatal parse error. There are two ways to fix it:
<?php
$myVar = 'How\'s it going?'; // escape the ' with a backslash
$myVar = "How's it going?"; // use " instead of ' to delimit the string
?>To display the contents of a (string or integer) variable on your webpage, you just echo it:
<?php
$myVar = 'Wuzzup, man?';
echo $myVar;
print $myVar; // note that print and echo do the same thing
?>To append something to an existing variable, use a dot. There are two ways to do this:
<?php
// method #1
$myVar1 = 'Hey dude. ';
$myVar1 .= 'How are you?; // $myVar1 now holds "Hey dude. How are you?"
// $method #2
$myVar2 = 'Hey man. ';
$myVar3 = 'How are you?';
$myVar4 = $myVar2 . $myVar3; // $myVar4 holds "Hey man. How are you?"
?>We’ll have a little more to say about variables in the next section.
9 comments »
Can this be used to display a different chunk of sidebar content depending on what page number you are on?
Yes, PHP can be used for that. See conditional tags.
I know I need to add the PHP file to the same directory as the page containing the PHP script but which FTP directory does Wordpress store the posts in pls?
@gadget: For general WP support, please use the WP support forums. The short answer: WP uses MySQL, not PHP, to store data.
Hello,
If the directory I am accessing from the site contains both index.php and index.htm ,What happens?
That’s not really a PHP question. It depends on your server configuration. But why ask? Just try it and see what happens.
Hello,
If I use single quote to assign a string to a variable in the PHP,Can I define another variable with a double quote? I mean, can I mix and match single and double quotes according to my convince in the same PHP snippet or should I stick to only one type of delimiters throughout the snippet?
Thanks a lot for the quick reply.
Hello,
Nice tutorial! simple,easy, neat, to the point.And the best part is it inspired me to learn a new web language.Thanks a lot
Thanks. Regarding your question about quotes, you can switch back and forth all you want. But again… why ask? Just try it and see what happens. That’s the way I learned PHP.
Leave a comment