PHP Questions Home | Register | Login
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website
Unanswered

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

How can I add branching logic to my PHP code?


By logic we mean if / else code, for instance if it is raining then take an umbrella, else don't.

This is achieved with the if and else statements in PHP, and it is quite simple to do.

Here is an example of this in action, where you will get a different message depending on whether it is raining or not.

<?php
$itisraining 
1//yes it is raining

if ($itisraining) { echo "It is raining, take an umbrella"; }
else { echo 
"No need for an umbrella-ella today!"; }
?>


Points to note: the curly braces { } enclose each section of logic, e.g. an if or an else code. These are not strictly mandatory and you'll often see them dropped for short pieces of logic on one line but it is best to use them for clarity and to stop confusion.

Run this program twice and the second time set the variable at the top to '0'. You will notice that the 'if' statement evaluates '0' to false, whereas '1' is true.



Comment on this Question and Answer >>>

ASK A QUESTION

More other PHP Questions

Can I spellcheck my text using PHP?

What are autoglobals?

How can I stop my PHP script timing out?

Can PHP create PDF files for me?

How do I define a constant in PHP?



Custom Search