Variables in Conditional Statements
Now it’s time to combine the two preceding sections. You learned above that you can assign $myVar the value Howdy! like so:
<?php $myVar = 'Howdy!'; ?>But what if you want to test whether $myVar already has the value Howdy!? To check a variables value, use two == instead of one =, like this:
<?php
// THE RIGHT WAY
if ($myVar == 'Howdy'){
echo 'Howdy!';
}
// THE WRONG WAY
if ($myVar = 'Howdy'){
echo 'Howdy!';
} ?>The first example is correct. The second will not work as expected. In the second example, the if is testing whether or not PHP is successfully able to assign the value of Howdy to $myVar, which will always be true.
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