Welcome, Guest

Please login or register

TUTORIALS SUBMENU ---->

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING

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
Featured Content

Abstract Wireframes
Abstract Wireframes
- Adobe Photoshop -
Quick Barcodes
Quick Barcodes
- Adobe Photoshop -
Custom Shapes
Custom Shapes
- Adobe Photoshop -
Layer Masks
Layer Masks
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Special Options
Printer Friendly Version
Forum Threads

Competition Discussion - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:48pm
Activity: 0 replies, 54 views
 Competition - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:46pm
Activity: 0 replies, 56 views
 PM Spamming
Author: Tamlin
Posted: Feb 06th, 1:24pm
Activity: 7 replies, 116 views
Vector Clipart Bank
Author: Crapoun
Posted: Feb 06th, 11:29am
Activity: 2 replies, 94 views
How did ...
Author: MoodsR4Cattle
Posted: Feb 05th, 6:09pm
Activity: 6 replies, 27 views
Tips and trick for Texturing/Materials
Author: noorjan
Posted: Feb 05th, 4:59am
Activity: 2 replies, 111 views
 A Billion Styles - Please Help Me!!
Author: Angelz
Posted: Feb 03rd, 6:36pm
Activity: 2 replies, 137 views
101 Things you didnt know in 3DS Max ...in fact...
Author: noorjan
Posted: Jan 31st, 6:04pm
Activity: 0 replies, 162 views
Pee Wee get's an IPad
Author: MoodsR4Cattle
Posted: Jan 30th, 4:25pm
Activity: 2 replies, 163 views
Spam :: Online hotel reservations for Hotels in...
Author: kieulinh
Posted: Jan 28th, 6:39am
Activity: 0 replies, 205 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 3 replies, 17 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 27 replies, 727 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    415