Learn Php From Basic | Php Interview Latest Notes

 Learn Php From Basic 




To Start PHP What You Should Already Know
 Php is Combination of HTML,CSS, JavaScript and PHP Tags 
you Must Know HTML, CSS, JavaScript 


PHP used to develop Static websites as well as Dynamic websites or Web applications. PHP stands for Hypertext Pre-processor, that earlier stood for Personal Home Pages
PHP scripts can only be interpreted on a server means PHP code is executed on the server( that has PHP installed) and PHP usually used to output HTML code to the browser. 


Php Latest Notes 2020-21



Uses of Php
It is used for create Dynamic Website. Interacting with web server (Apache etc.), Interacting with any back-end / Database Server e.g. MySQL Other Databases, To interaction with  file system of the OS
To implement the business logical layers (one or more).To Encrypt Data (various Ready functions are available in Php). Access Session variable and set Sessions , Cookies variable and set cookies
Using Php you can restrict user to access specific web page. Php Mail Function is used to send  E-Mails.  PHP is used to find today's date, and then build a calendar for the month. count your visitors on your website. PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them. It can handle forms, i.e. gather data from files, save data to a file.

Why use Php is Popular ? 

Php is open source free to use and distribute , Free Downloadable Simple and easy to Learn important is secured It runs on different platforms such as Windows, Linux, Unix, etc. It is compatible with almost all servers used today, such as Apache, IIS, etc. It supports many databases such as MySQL, Oracle, PostgreSQL etc.
It is perfectly suited for Web development and can be embedded directly into the HTML code.


Popular Application Developed in  PHP?

WordPress: It is powerful enough to be at the core of the biggest blogging system on the web 
Facebook: It is deep enough to run the largest social network (Facebook)!
Yahoo: Mailing Application
Wikipedia: Encyclopedia Application  
Tumblr: Small Blogging System 


Application of Php 

OpenCart, Zen Cart, Magento, PrestaShop, and Ubercart PHP Enterprise Software, Web Content Management Systems ,Customer Relationship Management Systems ,Enterprise Resource Planning Systems PHP for web database applications Mobile Apps

PHP frameworks 
PHP frameworks help to simplify the complex process of coding by providing a platform where developers can easily powerful apps in the shortest time possible.
Using PHP frameworks, therefore, saves time as well as the workload of having to start coding from scratch. There are many PHP frameworks available

Advantages of  PHP Framework?
  1. Speed : web pages developed using these frameworks are very responsive with fast loading speeds.
  2. Cross-platform: Computer and mobile as well as on different operating system 
  3. Greater Database Diversity
  4. Improved Stability
  5. Supports CMS
Top Php Framework
  1. Laravel
  2. Yii
  3. Symfony
  4. Codelgniter
  5. CakePHP
  6. Zend
  7. Phalcon
  8. FuelPHP
  9. Slim
  10. PHPixie


What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the browser as plain HTML
PHP files have extension ".Php"

which is  PHP Latest Version 2020? 
Ref: php.net
BranchInitial ReleaseActive Support UntilSecurity Support Until
7.36 Dec 20182 years ago6 Dec 202011 days ago6 Dec 2021in 11 months
7.428 Nov 20191 year ago28 Nov 2021in 11 months28 Nov 2022in 1 year, 11 months
8.026 Nov 202021 days ago26 Nov 2022in 1 year, 11 months26 Nov 2023in 2 years, 11 months



How to Start  PHP ? Requirement to Start Php ? 

Find a web host with PHP and MySQL support
Install a web server on your own PC, and then install PHP and MySQL
Use a Web Host With PHP Support

To develop a Php web application, you must first select the web server for hosting an application. which sets the base for the application, providing some built-in resources like databases, caching tools and other tools for website optimization

open source PHP server hosts available in the market having almost same functionalities. Many of them use Apache as the base server for hosting PHP MySQL applications


How to 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
The official PHP website (PHP.net) has installation instructions for PHP: http://php.net/manual/en/install.php


which are Best Open Source PHP Server Hosts ? 
  1. XAMPP
  2. WAMP
  3. LAMP
  4. LEMP
  5. MAMP
  6. AMPPS
  7. WPN-XM
  8. EasyPHP
  9. Final Words etc. 


How to use PHP Server Hosts  to Run Php ? 

create .php files, place them in your web directory in Xampp Server htdocs folder , and the server will automatically parse them for you.
No  need to compile anything or install any extra 

  • Install xampp Server on your pc on C: 
  • Save following File as one.php
<?php
$txt = "FreeSourcecode.in";
echo "I love $txt!";
?>
  • Put this file in C:\xampp\htdocs Folder 
  • Open Browser Google Chrome, IE, Mozella etc.  run using  localhost/one.php   



PHP Syntax
A PHP script is executed on the server, and the plain HTML result is sent back to the browser.

Basic PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php   and ends with ?>

<?php
// PHP code goes here 
?>
 extension for PHP files is ".php".  

A PHP file normally contains HTML tags, and some PHP scripting code.



<!DOCTYPE html>
<html>
<body>
<h1>Php Program </h1>
<?php
echo "Freesourcecode.i php  Notes ";
?>
</body>
</html>

Save This as  two.php  in htdocs folder  run in browser as use 
                       localhost/two.php   


What is PHP Comments

A comment is not part of the program. it is for understanding purpose for programmer 

Type of Comments in Php
  1.   single line Comment : used // or #
  2.   Multi-line Comment : /* multi line  */ 


  1. Single-line comments:

<!DOCTYPE html>
<html>
<body>

<?php
// This is a single-line comment
# This is also a single-line comment
?>
</body>
</html>


2. Multi-line comments:

<!DOCTYPE html>
<html>
<body>

<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>



What is PHP Variables? How To Create Php Variable ? 
How to Assign Value to php Variable?

Variables are "containers" for storing information i.e it store Constant.

Creating (Declaring) PHP Variables
We use $ sign to Create Php Variable , as follows


Eg.  
<?php
$txt = "FreeSourceCode php Programming Blog!";
$x = 5; // Assigning Number value to variable
$y = 10.5; // assigning float value to variable 
?>



What  is PHP Variables Scope ? 
PHP Variables Scope
In  you can Declare PHP, variables anywhere in the script.

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has three different variable scopes:
  1. global Variable
  2. local Variable
  3. static Variable
Global and Local Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:

Example
Variable with global scope:

<?php
$n = 15; // global scope
function myTest() {
  // using x inside this function will generate an error
  echo "<p>Variable n inside function is: $n</p>";
}
myTest();
echo "<p>Variable n outside function is: $n</p>";
?>

local Variable
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

Example
Variable with local scope:

<?php
function myTest() {
  $n= 5; // local scope
  echo "<p>Variable x inside function is: $n</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable n outside function is: $n</p>";
?>
You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

PHP The global Keyword
The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function):

<?php
$x = 5;
$y = 10;
function myTest() {
  global $x, $y;
  $y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.

The example above can be rewritten like this:

Example
<?php
$x = 5;
$y = 10;
function myTest() {
  $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>

PHP The static Keyword 
i. e. value persist in different call of function 
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted.

To do this, use the static keyword when you first declare the variable:

<?php
function myTest() {
  static $x = 0;
  echo $x;
  $x++;
}
myTest();
myTest();
myTest();
?>

PHP echo and print Statements
echo and print are two Php output statements.
this are used to display output on user screen
The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters & marginally faster than print.

The PHP echo Statement
The echo statement can be used with or without parentheses: echo or echo().



Example
<?php
echo "<h2>PHP Programming !</h2>";
echo "FreeSourcecode !<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>

Display Variables
The following example shows how to output text and variables with the echo statement:

Example
<?php
$txt1 = "PHP Programming";
$txt2 = "FreeSourcecode";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>

The PHP print Statement
The print statement can be used with or without parentheses: print or print().

The following example shows how to output text with the print command (notice that the text can contain HTML markup):

Example
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

The following example shows how to output text and variables with the print statement:

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>


Explain Data Types PHP in  Php
Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:
  1. String : Sequence of Characters like "Hello world!"
  2. Integer: to store whole Number .number between -2,147,483,648 and 2,147,483,647
  3. Float (floating point numbers - also called double): Number with decimal point 
  4. Boolean: to store  true or False i. e. 0 or 1
  5. Array: to store multiple values in one single variable  of same data type
  6. Object: objects, and an object is an instance of a class
  7. NULL :NULL

PHP String: A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:

Example
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>

PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

Rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation
In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

<?php
$n = 51985;
var_dump($n);
?>

PHP Float
A float (floating point number) is a number with a decimal point or a number in exponential form.
In the following example $x is a float. 
The PHP var_dump() function returns the data type and value:


<?php
$x = 10.365;
var_dump($x);
?>
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.

PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>


PHP Object
Classes and objects are the two main aspects of object-oriented programming.
A class is a template for objects, and an object is an instance of a class.When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Let's assume we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.

When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Example
<?php
class Car {
  public $color;
  public $model;
  public function __construct($color, $model) {
    $this->color = $color;
    $this->model = $model;
  }
  public function message() {
    return "My car is a " . $this->color . " " . $this->model . "!";
  }
}
$myCar = new Car("black", "Volvo");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>

PHP NULL Value
Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.

If a variable is created without a value, it is automatically assigned a value of NULL.

Variables can also be emptied by setting the value to NULL:

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>


What is PHP Constants ? 
Constants  Value Fixed Can not changed .

PHP Constants
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
 (no $ sign before the constant name
 Unlike variables, constants are automatically global across the entire script.

How to Create a PHP Constant ?
To create a constant, use the define() function.

Syntax
define(name, value, case-insensitive)
Parameters:

name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
Example

How to Create a constant with a case-sensitive name:

<?php
define("GREETING", "Welcome to 
freesourcecode.in!");
echo GREETING;
?>

How to Create a constant with a case-insensitive name ?

<?php
define("GREETING", "Welcome to freesourcecode.in!", true);
echo greeting;
?>


How to PHP Create an Array constant?
<?php
define("cars", [
  "Alfa Romeo",
  "BMW",
  "Toyota"
]);
echo cars[0];
?>

Constants are Global
Constants are automatically global and can be used across the entire script.


<?php
define("GREETING", "Welcome to freesourcecode.in!");
function myTest() {
  echo GREETING;
}
 myTest();
?>


PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Increment/Decrement operators
  5. Logical operators
  6. String operators
  7. Array operators
  8. Conditional assignment operators
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Name Example Result Show it
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y'th power

PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

Assignment Same as... Description Show it
x = y x = y  The left operand gets set to the value of the expression on the right
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus

PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result Show it
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
<=> Spaceship $x <=> $y Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.


PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value.

The PHP decrement operators are used to decrement a variable's value.

Operator Name Description Show it
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one

PHP Logical Operators
The PHP logical operators are used to combine conditional statements.

Operator Name Example Result Show it
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true


PHP String Operators
PHP has two operators that are specially designed for strings.

Operator Name Example Result Show it
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1
PHP Array Operators
The PHP array operators are used to compare arrays.

Operator Name Example Result Show it
+ Union $x + $y Union of $x and $y
== Equality $x == $y Returns true if $x and $y have the same key/value pairs
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y Returns true if $x is not equal to $y
<> Inequality $x <> $y Returns true if $x is not equal to $y
!== Non-identity $x !== $y Returns true if $x is not identical to $y

PHP Conditional Assignment Operators
The PHP conditional assignment operators are used to set a value depending on conditions:

Operator Name Example Result Show it
?: Ternary $x = expr1 ? expr2 : expr3 Returns the value of $x.
The value of $x is expr2 if expr1 = TRUE.
The value of $x is expr3 if expr1 = FALSE
?? Null coalescing $x = expr1 ?? expr2 Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Introduced in PHP 7




PHP if...else...elseif Statements
Conditional statements are used to perform different actions based on different conditions.

PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is true and another code if that condition is false
if...elseif...else statement - executes different codes for more than two conditions
switch statement - selects one of many blocks of code to be executed
PHP - The if Statement
The if statement executes some code if one condition is true.

Syntax
if (condition) {
  code to be executed if condition is true;
}
Example
Output "Have a good day!" if the current time (HOUR) is less than 20:

<?php
$t = date("H");

if ($t < "20") {
  echo "Have a good day!";
}
?>


PHP switch Statement
The switch statement is used to perform different actions based on different conditions.

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;
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.

Example
<?php
$favcolor = "red";
switch ($favcolor) {
  case "red":
    echo "Your favorite color is red!";
    break;
  case "blue":
    echo "Your favorite color is blue!";
    break;
  case "green":
    echo "Your favorite color is green!";
    break;
  default:
    echo "Your favorite color is neither red, blue, nor green!";
}
?>


PHP Loops
In the following chapters you will learn how to repeat code by using loops in PHP.

PHP Loops
Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.

Loops are used to execute the same block of code again and again, as long as a certain condition is true.

In PHP, we have the following loop types:

while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
The following chapters will explain and give examples of each loop type.


PHP while Loop
The while loop - Loops through a block of code as long as the specified condition is true.

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;
}
Examples
The example below displays the numbers from 1 to 5:

Example
<?php
$x = 1;
while($x <= 5) {
  echo "The number is: $x <br>";
  $x++;
}
?>
Example Explained
$x = 1; - Initialize the loop counter ($x), and set the start value to 1
$x <= 5 - Continue the loop as long as $x is less than or equal to 5
$x++; - Increase the loop counter value by 1 for each iteration
This example counts to 100 by tens:

Example
<?php
$x = 0;

while($x <= 100) {
  echo "The number is: $x <br>";
  $x+=10;
}
?>
Example Explained
$x = 0; - Initialize the loop counter ($x), and set the start value to 0
$x <= 100 - Continue the loop as long as $x is less than or equal to 100
$x+=10; - Increase the loop counter value by 10 for each iteration


PHP do while Loop
The do...while loop - Loops through a block of code once, and then repeats the loop as long as the specified condition is true.

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);
Examples
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);
?>
Note: In a do...while loop the condition is tested AFTER executing the statements within the loop. This means that the do...while loop will execute its statements at least once, even if the condition is false. See example below.

This example sets the $x variable to 6, then it runs the loop, and then the condition is checked:

Example
<?php
$x = 6;
do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);
?>


PHP for Loop
The for loop - Loops through a block of code a specified number of times.

The 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 for each iteration;
}
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
Examples
The example below displays the numbers from 0 to 10:

Example
<?php
for ($x = 0; $x <= 10; $x++) {
  echo "The number is: $x <br>";
}
?>
Example Explained
$x = 0; - Initialize the loop counter ($x), and set the start value to 0
$x <= 10; - Continue the loop as long as $x is less than or equal to 10
$x++ - Increase the loop counter value by 1 for each iteration
This example counts to 100 by tens:

Example
<?php
for ($x = 0; $x <= 100; $x+=10) {
  echo "The number is: $x <br>";
}
?>
Example Explained
$x = 0; - Initialize the loop counter ($x), and set the start value to 0
$x <= 100; - Continue the loop as long as $x is less than or equal to 100
$x+=10 - Increase the loop counter value by 10 for each iteration


PHP foreach Loop
The foreach loop - Loops through a block of code for each element in an array.

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.

Examples
The following example will output the values of the given array ($colors):

Example
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value <br>";
}
?>
The following example will output both the keys and the values of the given array ($age):

Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {
  echo "$x = $val<br>";
}
?>


PHP Break and Continue
PHP Break
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch statement.

The break statement can also be used to jump out of a loop.

This example jumps out of the loop when x is equal to 4:

Example
<?php
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
}
?>
PHP Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

This example skips the value of 4:

Example
<?php
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    continue;
  }
  echo "The number is: $x <br>";
}
?>
Break and Continue in While Loop
You can also use break and continue in while loops:

Break Example
<?php
$x = 0;

while($x < 10) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
  $x++;
}
?>
Continue Example
<?php
$x = 0;

while($x < 10) {
  if ($x == 4) {
    $x++;
    continue;
  }
  echo "The number is: $x <br>";
  $x++;
}
?>


Resources:
w3



Previous
Next Post »

Display Data Dynamically in Dropdown using Php MySQLi

 Display Data Dynamically in Dropdown using Php MySQLi Display Data Dynamically in Dropdown using Php MySQLi,  php Mysql Dropdown tutorial ,...

Article