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?"
?>I’ll have a little more to say about variables in the next section.
34 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.
Hey thanks for the help I am still not getting it to work but at least I understand a bit better now. Very clean and easy to read tutorial.
What I want to see happen is if somebody click about us the header changes and so does the sidebar to match the about us content ect. I will get there this was a good start, thinking it might just be easier to copy new directories onto my server lol.
Excellent tutorial. Just wanted to say thanks for taking the time to write it! :)
Hi Adam,
Thank you for your wonderful tutorial, it’s the best and the most relevant article that I have been looking for all this while!
However, may I know how do you insert all these textboxes? It’s so cool and I hope I can know more. Any recommendations?
Ah Doe: I think you’re looking for this:
[link]
I have download a template and run it in local host by using xampp, but when i try to get it the index.php, fatal error was occurred.
Fatal error: Call to undefined function language_attributes() in C:\xampp\htdocs\gear\index.php on line 2
I have try to do with php be4, but i never try to do WP template. Do i need any install extra stuff or kind of plugin or what to make WP source code can work in PHP.
I did try something else like get_header(); is_home(); bloginfo();
The same error message will appear, really hope that you can gv me some advice. Thanks very much.
genyou, this is not a wordpress support forum.
Nice php tutorial!
thanks!!!
Hi Adam,
Thanks for looking out for me. I really appreciate it.
Was looking and looking for a quick quide on PHP with wordpress and found your pages very very helpful.
Thanks for putting this together.
hello..how can i manipulate the custom fields and make its contents appear instead of the archive list?thanks
I found your website very helpful. Thanks for posting the info.
I am learning wordpress and want to change some functions. I must know PHP for that. In how many day of practice, I will be able to do PHP coding at my own.
Suman Sindhu: Hours, not days. Why not just read the tutorial?
Very helpful tutorial on PHP for Wordpress users.
Thanks for sharing.
Danke Adam!
Piecing together my first WP blog (integrating it into my site) I needed this…
Smooth transitions into learning curves (specifically php here) are often sorely lacking, so thanks for filling that gap.
I’ve got some more feedback (re return and print values) but I’ve got to go pick up my daughter now… more later!
Thanks!
…so smooth sailing but was momentarily thrown by the return and print values example
<?php is_home(); ?>returns a value, but<?php the_content(); ?>has no return valueWhich prompted me to look into it further and discovered the difference between conditional ([link]), and template, tags.
Thanks again!
I understand the conditional tag if (is_home())
I don’t know how can i do to return true in case of paged content in index.php. More specific for index i need true, and for the rest of the pages (page/2 , page/3 , page/4 , etc)=> false.
Something like a sticky post.
Can you help me?
From your example…
// method #1
$myVar1 = ‘Hey dude. ‘;
$myVar1 .= ‘How are you?; // $myVar1 now holds “Hey dude…
Appears to be a typo in line 3: no trailing apostrophe.
You are a good teacher!
Andrian: Wrong place. Go to wp.org and look up conditional tags.
John: Thanks, fixed.
I am trying to enter a PHP script into a wordpress page but whe i enter it wordpress makes part of it disipear. Please help
this is the code that i am trying to use. I have tried to download plugins that will allow me to enter a php code in a page but i cant get it to work.
<?php
$row = 1;
if (($handle = fopen("http://www.spc.noaa.gov/climo/reports/yesterday_torn.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "\n”;
}
}
fclose($handle);
}
?>
Chris, this isn’t a general WordPress support forum. That’s over at wordpress.org.
Great instruction, thanks.
But… I copied and pasted the time code on my blog page and saved it. When I visited the site, the code did not render. When I returned to design mode, the php tags had been changed to comment tags. What did I do wrong?
Jim: WordPress won’t execute PHP that you write into the editor. It strips it out unless you use a plugin that runs PHP found in blog posts. Those plugins exist (for example), but they’re usually not a good idea. PHP should go in your plugins and in your theme files.
Nice article for wordpress users who wish to do customization on their own.
But if you wish to just change the design or theme , you need to learn designing stuff too (HTML/CSS) etc,..
Yes, that’s why the article starts by saying that HTML and CSS are prerequisites.