Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

Introduction to PHP: Logical Operators

pages (2): [1] 2


Welcome to PHP. This tutorial is aimed at beginner PHP developers who want to learn how to use the PHP's in-built logical operators.

Let’s say you have two savings jars, one for notes and one for coins. A friend gives you some money. If it’s a note, you want put it in the notes jar, otherwise it must be a coin and goes with the coins. How do you code this in PHP? Well, we simply use an operator called IF!

Before we get any further, lets discuss the logic of operators. To start off you call the operator. Next you define the arguments.  Then you define the instructions that will be carried out when the arguments are correct. If you have more than one instruction to be processed in the event of the argument(s) being correct, you enclose them in curly brackets.  i.e.  { /*instructions*/ }.

If...Else Statements
Ok, let’s start with the IF operator. We will use our example above and code it. The logic is the same - i.e. if the money is a note, add it to the note box:
 

<?php

if($money == "note") $notes .= "<br />".$money;

?>

And it’s done. As you only have one instruction, you don’t need curly brackets. This script will check whether the variable $money is equal to the text “note”, and if so, adds a “<br />” and the value of $money to the $notes variable. <br /> is XHTML for a line drop, so when you output the variable you can see the individual entries.

.= is syntax that you can use instead of = [] + []. i.e. instead of typing $var = $var . $iable you can simply replace it with $var .= $iable. Other similar syntax formats you can use are: +=, -=, %=, *= and /=.

But what happens when $money is not equal to “note”?. To handle this, we use the ELSE operator:
 

<?php

if($money == "note"){
$notes .= "<br />".$money;
} else {
$coins .= "<br />".$money;
}

?>

The human translation of this code is quite simple: "if $money is equal to “note” then add the value of $money to the $notes variable, otherwise add it to the $coins variable".

To finish up our section on the IF statement, let’s look at how to define multiple conditions. We can use either && (and) or || (or) or simply the word “AND” or “OR”:
 

<?php

if( ($money == "note") || ($money == "coin") ) $piggybank .= $money;
# these are the same functions! (above and below)
if($money == "note" or $money == "coin") $piggybank .= $money;

?>

Of course, if you execute the above code, you will get the value of money appearing twice, because both statements are the same. That said, let’s move on to looping.

Looping
There are 3 main ways to use loop structures. The basic structure is: while [argument] is true, process instructions then re-iterate.

The first example I will outline uses WHILE, and is possibly the easiest looping structure. Let’s make a simple number generator that will output the numbers from 1 to 50 inclusive on a new line. Pleas note that <= means 'less than or equal to', >= means 'greater than or equal to', == means 'equal to', and != means 'not equal to'.

$i++ is a simple instruction, adding 1 to variable $i. You can also use $i += 1 or $i = $i + 1, if that is your way - it’s just preference really!
 

<?php

$i = 1;

while ($i <= 50)
{

echo $i . "<br />";
$i++;

}

?>

Simple enough? Good. Let’s move on to FOR statements. The FOR statement simply incorporates definition of the identifier, the argument and the last instruction into one line. The logic is: for [define identifier], [condition], [what to do with identifier] instructions. Let’s rewrite the above code with the FOR statement instead:
 

<?php

for($i = 1; $i <= 50; $i++) echo $i . "<br />";

?>

Note that I didn’t use curly brackets simply because there is only 1 instruction now. You can use them if you like, but it will not affect the output.

- Tutorial written by Scrowler

Pages (2): [1] 2 Next>
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Amazing Font Pack!

Featured Tutorialsmore

Signature Techniques
Signature Techniques
- Adobe Photoshop -
Displacement Maps
Displacement Maps
- Adobe Photoshop -
Simple Shape Tween...
Simple Shape Tween...
- Macromedia Flash -
The Pattern Maker
The Pattern Maker
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 867 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1037 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1042 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1919 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1816 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14417 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2712 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2752 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3427 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2344 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2482 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2792 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673