Saturday, May 3, 2014

PHP for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for (init counter; test counter; increment counter) {
  code to be executed;
}
Parameters:
  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value
The example below displays the numbers from 0 to 6:

Example

<?php
for ($x=0; $x<=6; $x++) {
  echo "The number is: $x <br>";
}
?> 
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6 



The PHP foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Syntax

foreach ($array as $value) {
  code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.
The following example demonstrates a loop that will output the values of the given array ($colors):

Example

<?php
$food = array("momo","pizza","chowmin" "Burger");

foreach ($food as $value) {
  echo "$value <br>";
}
?> 
Output:
momo
pizza
chowmin
Burger

PHP while and do while Loop

The PHP while Loop

The while loop executes a block of code as long as the specified condition is true.

Syntax

while (condition is true) {
  code to be executed;
}
The example below first sets a variable $x to 1 ($x=1;). Then, the while loop will continue to run as long as $x is less than, or equal to 8. $x will increase by 1 each time the loop runs ($x++;):

Example

<?php
$x=1;

while($x<=8) {
  echo "The number is: $x <br>";
  $x++;
}
?> 
 Ouptput:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5  
The number is: 6
The number is: 7
The number is: 8

The PHP do...while Loop

The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

Syntax

do {
  code to be executed;
} while (condition is true);
The example below first sets a variable $x to 1 ($x=1;). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

Example

<?php
$x=1;

do {
  echo "The number is: $x <br>";
  $x++;
} while ($x<=5);
?>
Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5 

PHP switch Statement

The PHP switch Statement

Use the switch statement to select one of many blocks of code to be executed.

Syntax

switch (n) {
  case label1:
    code to be executed if n=label1;
    break;
  case label2:
    code to be executed if n=label2;
    break;
  case label3:
    code to be executed if n=label3;
    break;
  ...
  default:
    code to be executed if n is different from all labels;
Example:
<?php
$number = -10

switch ($number) {
  case 1:
     echo "Your number is one!";
    break;
  case 2:
     echo "Your number is two!";
    break;
  case 3:
    echo "Your number is three!";
    break;
  default:
    echo "Number not Found!";
}
?> 

Output: Number not Found!

PHP if...else...elseif Statements

In PHP we have the following conditional statements:
  • if statement - executes some code only if a specified condition is true
  • if...else statement - executes some code if a condition is true and another code if the condition is false
  • if...elseif....else statement - selects one of several blocks of code to be executed 

PHP - The if Statement
The if statement is used to execute some code only if a specified condition is true.

Syntax

if (condition) {
  code to be executed if condition is true
;
}
The example below will output "Have a good day!" if the current time (HOUR) is less than 20:

Example

<?php
$text = 'Something';
if($text=='Something else'){
echo 'TRUE';
}else{
echo 'FALSE';
}
Output:
FALSE

PHP - The if...else if Statement
Use the if....else statement to execute some code if a condition is true and another code if the condition is false.

Syntax

if (condition) {
  code to be executed if condition is true;
} else {
  code to be executed if condition is false;
}
The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:

Example

<?php
$number = 11;

if($number==10){
 echo 'Equal to 10';
}else if ($number==11){
 echo 'Equal to 11';
}else{
 echo 'Not equal';
}
?>
Output:Equal to 11
 

PHP Operators

PHP - Operators

In all programming languages, operators are used to manipulate or perform operations on variables and values. The assignment operator "=" in pretty much every PHP example so far.

There are mainly five types of Operators:-
  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • String Operators
  • Combination Arithmetic & Assignment Operators

Assignment operators : It used to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the "=", or equal character. Example:
  • $my_var = 4;
  • $another_var = $my_var; 

PHP Code:

$number1 = 10;
$number1 +=2;  //including +-*/% and all other
echo $number1;

Output:

12
8
20
5
0

Comparison Operators

Comparisons are used to check the relationship between variables and/or values. Comparison operators are used inside conditional statements and evaluate to either true or false. Here are the most important comparison operators of PHP.
Assume: $x = 4 and $y = 5;

String Operators

As we have already seen in the previous Lesson "." is used to add two strings together, or more technically, the period is the concatenation operator for strings.

PHP Code:

$a_string = "Hello";
$another_string = " Billy";
$new_string = $a_string . $another_string;
echo $new_string . "!";

Output:

Hello Billy! 

Arithmetic Operators

PHP Code:

<?php

$number1 = 100;

$number2 = 50;

$number3 = 2;

$result = ($number1+$number2)/$number3;

echo $result;

?>

Pre/Post-Increment & Pre/Post-Decrement

This may seem a bit absurd, but there is even a shorter shorthand for the common task of adding 1 or subtracting 1 from a variable. To add one to a variable or "increment" use the "++" operator:

PHP Code:

$x = 4;
echo "The value of x with post-plusplus = " . $x++;
echo "<br /> The value of x after the post-plusplus is " . $x;
$x = 4;
echo "<br />The value of x with with pre-plusplus = " . ++$x;
echo "<br /> The value of x after the pre-plusplus is " . $x;

Output:

The value of x with post-plusplus = 4
The value of x after the post-plusplus is = 5
The value of x with with pre-plusplus = 5
The value of x after the pre-plusplus is = 5




 

Friday, May 2, 2014

PHP echo

PHP - Echo

As you saw in the previous lesson, the PHP command echo is a means of outputting text to the web browser. Throughout your PHP career you will be using the echo command more than any other. So let's give it a solid perusal!

PHP Code:

<?php 
$myString = "Hello!";
echo $myString;
echo "<h5>I love using PHP!</h5>";
?>

Display:

Hello!
I love using PHP!

Echoing Variables

Echoing variables is very easy. The PHP developers put in some extra work to make the common task of echoing all variables nearly foolproof! No quotations are required, even if the variable does not hold a string. Below is the correct format for echoing a variable.

PHP Code:

<?php
$my_string = "Hello Bob.  My name is: ";
$my_number = 4;
$my_letter = a;
echo $my_string;
echo $my_number;
echo $my_letter;
?>

Display:

Hello Bob. My name is: 4a
 
 
 

PHP Variables

PHP - Variables

If you have never had any programming, Algebra, or scripting experience, then the concept of variables might be a new concept to you.
A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again. In PHP you define a variable with the following form:
  • $variable_name = Value;
If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!

PHP Code:

<?php

$x=5;

$y=6;

$z=$x+$y;

echo $z;

?> 
 
Output:11

Declaring PHP Variables:

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

Code: 

<?php
$txt="Hello world!";
$x=5;
$y=10.5;
echo $txt;
echo $x;
echo $y;
?> 
Output:
Hello world!
5
10.5 

  Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case sensitive ($y and $Y are two different variables)