Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP: Conditionals
#1
If you need help: feel free to PM me.
Credits to: PHP, MySQL & JavaScript by O'Reilly and Tizag tutorials.

[Image: YzwzC.png]

Introduction:
Conditionals alter program flow and enable you to ask questions about certain things and respond to the answers you get in various ways. Conditionals are central to dynamic web pages - the goal of using PHP in the first place - because they make it easy to create different output each time a page is viewed.

==

The if Statement:
One way of thinking about program flow is to imagine it as a single-lane highway that you are driving along. It's pretty much a straight line, but now and then you'll encounter various signs telling you where to go.
Now imagine coming across a detour sign that you have to follow if a certain condition is TRUE, you ignore the detour and carry on driving.

The contents of the if condition can be any valid PHP expression, including equality, comparison, tests for zero and NULL, and even the values returned by functions.

The action to take when an if conditions is TRUE are generally placed inside curly braces, { }. You can however ignore the braces if you have only a single statement to execute. It is a good practice, though, so I recommend using them.

Example:

PHP Code:
<?php
$day 
"Friday"//Assigning Friday to the day variable

if ($day == "Friday"){
   echo 
"It's Friday, Friday, gotta get down' on Friday!"
}
?>

Alright, let's analyze my example. In this example, your conditional is if the day variable equals to Friday, it would echo out "It's Friday, Friday, gotta get down' on Friday!"
If the condition is not met, nothing will be echo'd in this example.
So basically, if your conditional is met, it'll do whatever is in the curly braces.

Note:
  • In PHP, you use the double equal sign (==) to compare values.
  • You use the single equal sign to assign values.

The else Statement:
Sometimes when a conditional is not TRUE, you may not want to continue on to the main program code immediately but might wish to do something else instead. With the else statement, you can set up a second detour on your highway.

What happens with an if...else statement is that the first conditional statement is executed if the condition is TRUE, but if it's FALSE, the second one is executed. Only one of the two choices can be executed, under no circumstances can both (or neither) be executed.

Example:

PHP Code:
<?php
$day 
"Friday"

if(
$day == Friday){
   echo 
"It's Friday, Friday, gotta get down' on Friday!";
}
else
{
   echo 
"G'day sir!";
}
?>

In this example, if your condition isn't met, it will execute your else statement, which echos out "G'day sir!".

The elseif Statement:
There are also times when you want a number of different possibilities to occur, based upon a sequence of conditions. You can achieve this using the elseif statement. It is like an else statement, except that you place a further conditional expression prior to the condition code.

Example:

PHP Code:
<?php
$day 
"Friday";

if (
$day == Friday){
    echo 
"It's Friday, Friday, gotta get down' on Friday!";
}
elseif (
$day == Saturday){
          echo 
"It's Saturday";
}
elseif (
$day == Monday){
          echo 
"God dammit";
}
?>

Switch Statements:
With the use of the switch statement you can check for all these else...if conditions at once, and the great thing is that it is actually more efficient programming to do this. A true win-win situation!

The way the Switch statement works is it takes a single variable as input and then checks it against all the different cases you set up for that switch statement. Instead of having to check that variable one at a time, as it goes through a bunch of If Statements, the Switch statement only has to check one time.

Example:

In our example the single variable will be $day and the cases will be: Friday, Saturday, and Monday.

PHP Code:
<?php
$day 
"Friday";
switch (
$day)
{

     case 
"Friday": echo "It's Friday, Friday, gotta get down' on Friday!";
             break;
     case 
"Saturday": echo "It's Saturday";
             break;
     case 
"Monday": echo "God dammit";
             break;
}
?>

Note:
  • The break command tells PHP to break out of the switch and jump to the following statement. If you were to leave the break commands out, and the case of Friday evaluated to TRUE, all the other cases would then be executed.
Reply
#2
Wow these are similar to C++/VB.
Thanks for this.
If you are willing to join SF Webmasters.
Reply
#3
Kewlz.
The sintaxis of php, c++, c#, java and pascal (maybe more) is similar.

vb is too different to these, but in the use of variables not
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 939 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,664 10-10-2011, 01:00 PM
Last Post: Greyersting

Forum Jump:


Users browsing this thread: 1 Guest(s)