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)
 
 
 

 


PHP Comments

PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments.
For example:
 <?php

 /* This will not print on the screen as it is a Comment.
   

*/

 //Uses echo language construct to output the text "Hello World." to the browser

echo 'Hello World'
?>
 
The Hello World will only be printed on the screen as it is not a comment.It is neither single line comment nor multiline comment.The above code is used by the programmer in order to check the errors in the Coding and to debug the PHP Coding.
Example:

Your First "Hello World" File in PHP

TEXT EDITOR FOR PHP
There are many text editor software available to code the PHP in the local webserver.Some of the text editor  to code  PHP are as follows:-
  • Notepad++
  • con Text
  • Dreamweaver
  • Notepad

 You can download one of the software like conTEXT from the following link:www.contexteditor.org/‎
After conTEXT or  Notepad++ you can start coding:

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function "echo" to output the text "Hello World!" on a web page:

Example:
 <?php
echo "Hello World!";
?>

 We need to save this file inside the XAMMP directory  inside the folder name htdocs.
You need to save the file by putting .php after the name inside the htdocs Folder

After this we need to run this in our browser and check every code we write to know whether the written code is right or wrong.




 

Installation of XAMPP

What Do I Need?

To start using PHP, you can:
  • Find a web host with PHP and MySQL support
  • Install a web server on your own PC, and then install PHP and MySQL

Set Up PHP on Your Own PC
However, if your server does not support PHP, you must:
  • install a web server
  • install PHP
  • install a database, such as MySQL
  • for that install XAMPP or WAMP.

The website to download local webserver in your own computer  for that you can visit: https://www.apachefriends.org/index.html.
After downloading the XAMPP from the above website you need to unzip the file and extract in any new directory.Click the Next button to continue the installation process. As with most wizard-like
installations, you are asked to select an installation location and some installation
options before moving to the next step. The XAMPP installation is no different; you
should leave the default installation location and the default installation options as
marked and click the Next button to move on past each screen. At this point, the
installation process itself happens, as shown in Figure:



The XAMPP installation is complete.


After Installation of XAMPP we need to run the Control panel Application as shown in figure
We can get the Welcome Message by typing local host or 127.0.0.1 in any browser as shown in figure:

 Go to phpMyAdmin located below the tool option in XAMPP to enter MySQL Database


Introduction to PHP

What is PHP?
 PHP ( Hypertext Preprocesser) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.It is used to create a dynamic webpages and cannot be viewed by the user.It is behind the screen coding language which cannot be viewed from the source code view from the user.It must be manipulated by the web server in order to be executed.
For example:
<html>
<head>
<title>Example</title>
 </head>
 <body>
<?php
 echo "Hi, I'm a PHP script!";
 ?>

</body>
</html>


The best things in using PHP are that it is extremely simple for a newcomer, but offers many advanced features for a professional programmer. Don't be afraid reading the long list of PHP's features. You can jump in, in a short time, and start writing simple scripts in a few hours.

Common uses of PHP:
  • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • PHP can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user.
  • You add, delete, modify elements within your database through PHP.
  • Access cookies variables and set cookies.
  • Using PHP, you can restrict users to access some pages of your website.
  • It can encrypt data.
    For more information on PHP we can visit the Offical link:http://www.php.net/