PHP BASICS
 DataType
 Variable
 Constant
 Operators
 Control and Looping structure
 Arrays
 PHP Errors
 Include vs Require
By
www.Creativedev.in
DATATYPE
A Data type refer to the type of data a variable can store. A Data type is
determined at runtime by PHP. If you assign a string to a variable, it
becomes a string variable and if you assign an integer value, the variable
becomes an integer variable.
CONTINUE...
PHP supports eight primitive types:
Four scalar types:
1. Boolean
2. integer
3. float (floating-point number, aka double)
4. string
Two compound types:
5. array
6. object
And finally three special types:
7. resource
8. NULL
9. callable
CONTINUE...
Integer : used to specify numeric value
SYNTAX:
<?php $variable = 10; ?>
Float : used to specify real numbers
SYNTAX:
<?php
$variable = -10;
$variable1 = -10.5;
?>
Boolean: values true or false, also 0 or empty.
SYNTAX:
<?php $variable = true; ?>
CONTINUE...
String: sequence of characters included in a single or double quote.
SYNTAX:
<?php
$string = 'Hello World';
$string1 = "Hellon World";
?>
VARIABLE
 Variables are used for storing a values, like text strings, numbers or
arrays.
 All variables in PHP start with a $ symbol.
SYNTAX:
$var_name = value;
Example:
$name = 'Bhumi';
VARIABLE SCOPE
Scope can be defined as the range of availability a variable has to the
program in which it is declared. PHP variables can be one of four scope
types:
1) Local variables
2) Function parameters
3) Global variables
4) Static variables
VARIABLE NAMING RULES
1) A variable name must start with a letter or an underscore "_". Ex,
$1var_name is not valid.
2) Variable names are case sensitive; this means $var_name is different
from $VAR_NAME.
3) A variable name should not contain spaces. If a variable name is more
than one word, it should be separated with underscore. Ex, $var
name is not valid,use $var_name
VARIABLE VARIABLES
Variable variables allow you to access the contents of a variable without
knowing its name directly - it is like indirectly referring to a variable
EXAMPLE:
$a = 'hello';
$$a = 'world';
echo "$a $hello";
VARIABLE TYPE CASTING
Type casting is converting a variable or value into a desired data type.
This is very useful when performing arithmetic computations that require
variables to be of the same data type. Type casting in PHP is done by the
interpreter.
PHP also allows you to cast the data type. This is known as Explicit
casting. The code below demonstrates explicit type casting.
VARIABLE TYPE CASTING EXAMPLE
<?php
$a = 1;
$b = 1.5;
$c = $a + $b;
$c = $a + (int) $b;
echo $c;
?>
CONSTANTS
 A constant is an identifier (name) for an unchangable value.
 A valid constant name starts with a letter or underscore (no $ sign before
the constant name).
Note: Unlike variables, constants are automatically global across the entire
application.Constants are usually In UPPERCASE.
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
CONSTANT EXAMPLES
<?php
define('NAME','Bhumi');
echo NAME;
?>
PHP OPERATORS
1. Arithmetic Operators
OPERATOR DESCRIPTION EXAMPLE RESULT
+ Addition x=2,x+2 4
- Subtraction x=2,5-x 3
* Multiplication x=4,x*5 20
/ Division 15/5,5/2 3
2.5
% Modulus(division
remainder)
5%2,10%8 1
2
++ Increment x=5,x++ 6
-- Decrement x=5,x-- 4
PHP OPERATORS
2. Assignment Operators
OPERATOR EXAMPLE IS THE SAME AS
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= X%=y x=x%y
PHP OPERATORS...
3. Logical Operators
OPERATOR DESCRIPTION EXAMPLE
&& and x=6,y=3
(x < 10 && y > 1)
return true
|| OR x=6,y=3
(x==5 || y==5)
return false
! not x=6,y=3
!(x==y)
return true
PHP OPERATORS
4. Comparison Operators
OPERATOR DESCRIPTION EXAMPLE
== is equal to 5==8 return false
!= is not equal 5!=8 return true
<> is not equal 5<>8 return true
> is greater than 5>8 return false
< is less than 5<8 return true
>= is greater than or equal
to
5>=8 return false
<= is less than or equal to 5<=8 return true
CONTINUE...
5. Ternary Operator
? is represent the ternary operator
SYNTAX:
(expr) ? if_expr_true : if_expr_false;
expression evaluates TRUE or FALSE
TRUE: first result (before colon)
FALSE: second one (after colon)
CONTINUE
Ternary Operator Example
<?php
$a = 10;
$b = 13;
echo $a < $b ? 'Yes' : 'No';
?>
CONTROL AND LOOPING STRUCTURE
1. If/else
2. Switch
3. While
4. For loop
5. Foreach
IF/ELSE STATEMENT
Here is the example to use if/else statement
<?php
// change message depending on whether
// number is less than zero or not
$number = -88;
if ($number < 0) {
echo 'That number is negative';
} else {
echo 'That number is either positive or zero';
}
?>
THE IF-ELSEIF-ELSE STATEMENT
<?php
// handle multiple possibilities
if($answer == ‘y’) {
print "The answer was yesn";
else if ($answer == ‘n’) {
print "The answer was non";
}else{
print "Error: $answer is not a valid answern";
}
?>
THE SWITCH-CASE STATEMENT
<?php
// handle multiple possibilities
switch ($answer) {
case 'y':
print "The answer was yesn";
break;
case 'n':
print "The answer was non";
break;
default:
print "Error: $answer is not a valid answern";
break;
}
?>
WHILE LOOP
SYNTAX
while (condition):
code to be executed;
endwhile;
EXAMPLE
<?php
// repeats until counter becomes 10
$counter = 1;
while($counter < 10){
echo $counter;
$counter++;
}
?>
THE DO-WHILE LOOP
SYNTAX:
do {
code to be executed;
} while (condition);
EXAMPLE
<?php
// repeats until counter becomes 10
$counter = 1;
do{
echo $counter;
$counter++;
}while($counter < 10);
?>
THE FOR LOOP
SYNTAX
for (init; cond; incr)
{
code to be executed;
}
init: Is mostly used to set a counter, but can be any code to be executed
once at the beginning of the loop statement.
cond: Is evaluated at beginning of each loop iteration. If the condition
evaluates to TRUE, the loop continues and the code executes. If it
evaluates to FALSE, the execution of the loop ends.
incr: Is mostly used to increment a counter, but can be any code to be
executed at the end of each loop.
BREAKING A LOOP
Occasionally it is necessary to exit from a loop before it has met whatever
completion criteria were specified. To achieve this, the break statement
must be used. The following example contains a loop that uses the break
statement to exit from the loop when i = 100, even though the loop is
designed to iterate 100 times:
for ($i = 0; $i < 100; $i++)
{
if ($i == 10)
{
break;
}
}
CONTINUE
Skipping Statements in Current Loop Iteration.
continue is used within looping structures to skip the rest of the current
loop iteration and continue execution at the condition evaluation and
then the beginning of the next iteration.
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$in";
}
EXAMPLE:
<?php
// repeat continuously until counter becomes 10
// output:
for ($x=1; $x<10; $x++) {
echo "$x ";
}
?>
ARRAY IN PHP
An array can store one or more values in a single variable
name.
There are three different kind of arrays:
1. Numeric array - An array with a numeric ID key
2. Associative array - An array where each ID key is associated with a
value
3. Multidimensional array - An array containing one or more arrays
FOREACH LOOP
SYNTAX:
foreach (array as value)
{
code to be executed;
}
<?php
$fruits = array(
'a' => 'apple',
'b' => 'banana',
'p' => 'pineapple',
'g' => 'grape');
?>
FOREACH LOOP
$array = ['apple', 'banana', 'orange‘];
foreach ($array as $value) {
$array = strtoupper($value);
}
foreach ($array as $key => $value) {
$array[$key] = strtolower($value);
}
var_dump($array);
array(3) {
[0]=> string(3) "APPLE"
[1]=> string(3) "BANANA"
[2]=> &string(3) "ORANGE"
}
ERRORS AND ERROR MANAGEMENT
Errors:
Basically errors can be of one of two types
External Errors
Logic Errors (Bugs)
What about these error types?
External Errors will always occur at some point or another
External Errors which are not accounted for are Logic Errors
Logic Errors are harder to track down
PHP ERRORS
Four levels of error condition to start with
• Strict standard problems (E_STRICT)
• Notices (E_NOTICE)
• Warnings (E_WARNING)
• Errors (E_ERROR)
To enable error in PHP :
ini_set('display_errors', 1);
error_reporting(E_ALL);
PHP ERRORS EXAMPLE
// E_NOTICE :
<?php echo $x = $y + 3; ?>
Notice: Undefined variable: y
// E_WARNING
<?php $fp = fopen('test_file', 'r'); ?>
Warning: fopen(test_file): failed to open stream: No such file or directory
// E_ERROR
<?php NonFunction(); ?>
Fatal error: Call to undefined function NonFunction()
REQUIRE, INCLUDE
1. require('filename.php');

Include and evaluate the specified file

Fatal Error
2. include('filename.php');

Include and evaluate the specified file

Warning
3. require_once/include_once

If already included,won't be include again
TUTORIAL
1. Write a PHP script using a ‘do while’ loop that accept value from two
input box and perform addition, subtraction, multiplication,
division,modulus, square-root, square, Factorial operation on two value
then display total as the output.
Example:
First value input field + select box for operator selection + second value =
OUTPUT
2.Create a php function that accepts an integer value and other
information like name and outputs a message based on the number
entered by user in input field. Use a ‘switch’ statement for interger and
display values.
For a remainder of 0, print: “Welcome ”, [name],
For a remainder of 1, print: “How are you,[name]?”
For a remainder of 2, print: “I’m doing well, Thank you”
For a remainder of 3, print: “Have a nice day”
For a remainder of 4, print: “Good-bye”
3. Write a PHP program that display series of numbers (1,2,3,4, 5....etc)
in an infinite loop. The program should quit if someone hits a specific
ESCAPE key.
4. Create a PHP Program that Use an associative array to assign for
person name with their favourite color and print “x favourite color is y”
using foreach loop in HTML table
5. Create a PHP program that get system date and convert it in different
formats with usingdisplay in Indian Timezone.
1. 29-Jun-2015
2. 06 29 2013 23:05 PM
3. 29th June 2015 4:20:01
4. Tomorrow
5. Get the date of Next week from today
6. Get the date of Next monday
6.Create a Program which accept Year from selectbox and display
computed age based on the Selected Value.

More Related Content

PPTX
Introduction to php
PPT
Requirements analysis
PPT
CSS Basics
PPTX
Father to son Class 11 English
PPT
Free fall
PPTX
PHP slides
PDF
Introduction To Computer
PDF
Introduction to Microsoft Azure Cloud
Introduction to php
Requirements analysis
CSS Basics
Father to son Class 11 English
Free fall
PHP slides
Introduction To Computer
Introduction to Microsoft Azure Cloud

What's hot (20)

PPT
PHP - Introduction to Object Oriented Programming with PHP
PDF
4.2 PHP Function
PPSX
Php and MySQL
PPSX
Introduction to Html5
PPT
Control Structures In Php 2
PPTX
ASP.NET Page Life Cycle
PPTX
PHP Cookies and Sessions
PPT
Php Presentation
PPTX
Lesson 6 php if...else...elseif statements
PPTX
Data types in php
PPT
Javascript arrays
PPT
PHP variables
PDF
Php array
PPTX
Statements and Conditions in PHP
PPT
PDF
PHP Loops and PHP Forms
PDF
Control Structures in Python
PPTX
Form Validation in JavaScript
PDF
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
PHP - Introduction to Object Oriented Programming with PHP
4.2 PHP Function
Php and MySQL
Introduction to Html5
Control Structures In Php 2
ASP.NET Page Life Cycle
PHP Cookies and Sessions
Php Presentation
Lesson 6 php if...else...elseif statements
Data types in php
Javascript arrays
PHP variables
Php array
Statements and Conditions in PHP
PHP Loops and PHP Forms
Control Structures in Python
Form Validation in JavaScript
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
Ad

Viewers also liked (13)

PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
PPT
What Is Php
 
PDF
Ruby Egison
PPTX
Canvas in html5
PPT
Network Security Through FIREWALL
PPT
Introduction to PHP Basics
PPTX
Ruby Programming Language - Introduction
PPTX
Looping statement
PPT
Chapter 05 looping
PPTX
Looping and switch cases
PPT
Ruby Basics
 
PPT
Introduction To PHP
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
What Is Php
 
Ruby Egison
Canvas in html5
Network Security Through FIREWALL
Introduction to PHP Basics
Ruby Programming Language - Introduction
Looping statement
Chapter 05 looping
Looping and switch cases
Ruby Basics
 
Introduction To PHP
Ad

Similar to PHP - DataType,Variable,Constant,Operators,Array,Include and require (20)

PPTX
unit 1.pptx
PPSX
Php using variables-operators
PDF
How to run PHP code in XAMPP.docx (1).pdf
ODP
OpenGurukul : Language : PHP
PPTX
Learn PHP Basics
PPT
Web Technology_10.ppt
PPTX
FYBSC IT Web Programming Unit IV PHP and MySQL
PPT
Introduction to PHP
PPTX
PHP Powerpoint -- Teach PHP with this
PPTX
PHP PPT FILE
PDF
03phpbldgblock
PPTX
Php introduction
PPT
Php Chapter 1 Training
PPTX
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PPT
Introduction To Php For Wit2009
PDF
Web 8 | Introduction to PHP
PPTX
Expressions and Operators.pptx
ODP
PHP Basic
PPT
Php essentials
unit 1.pptx
Php using variables-operators
How to run PHP code in XAMPP.docx (1).pdf
OpenGurukul : Language : PHP
Learn PHP Basics
Web Technology_10.ppt
FYBSC IT Web Programming Unit IV PHP and MySQL
Introduction to PHP
PHP Powerpoint -- Teach PHP with this
PHP PPT FILE
03phpbldgblock
Php introduction
Php Chapter 1 Training
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
Introduction To Php For Wit2009
Web 8 | Introduction to PHP
Expressions and Operators.pptx
PHP Basic
Php essentials

More from TheCreativedev Blog (6)

PPT
Node.js Express Framework
PPT
Node js Modules and Event Emitters
PPT
Node.js Basics
PPTX
Post via e mail in word press
PPTX
Post via e mail in WordPress
PPTX
TO ADD NEW URL REWRITE RULE IN WORDPRESS
Node.js Express Framework
Node js Modules and Event Emitters
Node.js Basics
Post via e mail in word press
Post via e mail in WordPress
TO ADD NEW URL REWRITE RULE IN WORDPRESS

Recently uploaded (20)

PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PPTX
Chapter 5: Probability Theory and Statistics
PDF
CloudStack 4.21: First Look Webinar slides
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
Five Habits of High-Impact Board Members
PPT
What is a Computer? Input Devices /output devices
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Two-dimensional Klein-Gordon and Sine-Gordon numerical solutions based on dee...
PDF
Zenith AI: Advanced Artificial Intelligence
DOCX
search engine optimization ppt fir known well about this
A proposed approach for plagiarism detection in Myanmar Unicode text
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Final SEM Unit 1 for mit wpu at pune .pptx
1 - Historical Antecedents, Social Consideration.pdf
The influence of sentiment analysis in enhancing early warning system model f...
sustainability-14-14877-v2.pddhzftheheeeee
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Consumable AI The What, Why & How for Small Teams.pdf
Chapter 5: Probability Theory and Statistics
CloudStack 4.21: First Look Webinar slides
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Five Habits of High-Impact Board Members
What is a Computer? Input Devices /output devices
Enhancing emotion recognition model for a student engagement use case through...
Module 1.ppt Iot fundamentals and Architecture
A comparative study of natural language inference in Swahili using monolingua...
Two-dimensional Klein-Gordon and Sine-Gordon numerical solutions based on dee...
Zenith AI: Advanced Artificial Intelligence
search engine optimization ppt fir known well about this

PHP - DataType,Variable,Constant,Operators,Array,Include and require

  • 1. PHP BASICS  DataType  Variable  Constant  Operators  Control and Looping structure  Arrays  PHP Errors  Include vs Require By www.Creativedev.in
  • 2. DATATYPE A Data type refer to the type of data a variable can store. A Data type is determined at runtime by PHP. If you assign a string to a variable, it becomes a string variable and if you assign an integer value, the variable becomes an integer variable.
  • 3. CONTINUE... PHP supports eight primitive types: Four scalar types: 1. Boolean 2. integer 3. float (floating-point number, aka double) 4. string Two compound types: 5. array 6. object And finally three special types: 7. resource 8. NULL 9. callable
  • 4. CONTINUE... Integer : used to specify numeric value SYNTAX: <?php $variable = 10; ?> Float : used to specify real numbers SYNTAX: <?php $variable = -10; $variable1 = -10.5; ?> Boolean: values true or false, also 0 or empty. SYNTAX: <?php $variable = true; ?>
  • 5. CONTINUE... String: sequence of characters included in a single or double quote. SYNTAX: <?php $string = 'Hello World'; $string1 = "Hellon World"; ?>
  • 6. VARIABLE  Variables are used for storing a values, like text strings, numbers or arrays.  All variables in PHP start with a $ symbol. SYNTAX: $var_name = value; Example: $name = 'Bhumi';
  • 7. VARIABLE SCOPE Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types: 1) Local variables 2) Function parameters 3) Global variables 4) Static variables
  • 8. VARIABLE NAMING RULES 1) A variable name must start with a letter or an underscore "_". Ex, $1var_name is not valid. 2) Variable names are case sensitive; this means $var_name is different from $VAR_NAME. 3) A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore. Ex, $var name is not valid,use $var_name
  • 9. VARIABLE VARIABLES Variable variables allow you to access the contents of a variable without knowing its name directly - it is like indirectly referring to a variable EXAMPLE: $a = 'hello'; $$a = 'world'; echo "$a $hello";
  • 10. VARIABLE TYPE CASTING Type casting is converting a variable or value into a desired data type. This is very useful when performing arithmetic computations that require variables to be of the same data type. Type casting in PHP is done by the interpreter. PHP also allows you to cast the data type. This is known as Explicit casting. The code below demonstrates explicit type casting.
  • 11. VARIABLE TYPE CASTING EXAMPLE <?php $a = 1; $b = 1.5; $c = $a + $b; $c = $a + (int) $b; echo $c; ?>
  • 12. CONSTANTS  A constant is an identifier (name) for an unchangable value.  A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire application.Constants are usually In UPPERCASE. 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
  • 14. PHP OPERATORS 1. Arithmetic Operators OPERATOR DESCRIPTION EXAMPLE RESULT + Addition x=2,x+2 4 - Subtraction x=2,5-x 3 * Multiplication x=4,x*5 20 / Division 15/5,5/2 3 2.5 % Modulus(division remainder) 5%2,10%8 1 2 ++ Increment x=5,x++ 6 -- Decrement x=5,x-- 4
  • 15. PHP OPERATORS 2. Assignment Operators OPERATOR EXAMPLE IS THE SAME AS = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y .= x.=y x=x.y %= X%=y x=x%y
  • 16. PHP OPERATORS... 3. Logical Operators OPERATOR DESCRIPTION EXAMPLE && and x=6,y=3 (x < 10 && y > 1) return true || OR x=6,y=3 (x==5 || y==5) return false ! not x=6,y=3 !(x==y) return true
  • 17. PHP OPERATORS 4. Comparison Operators OPERATOR DESCRIPTION EXAMPLE == is equal to 5==8 return false != is not equal 5!=8 return true <> is not equal 5<>8 return true > is greater than 5>8 return false < is less than 5<8 return true >= is greater than or equal to 5>=8 return false <= is less than or equal to 5<=8 return true
  • 18. CONTINUE... 5. Ternary Operator ? is represent the ternary operator SYNTAX: (expr) ? if_expr_true : if_expr_false; expression evaluates TRUE or FALSE TRUE: first result (before colon) FALSE: second one (after colon)
  • 19. CONTINUE Ternary Operator Example <?php $a = 10; $b = 13; echo $a < $b ? 'Yes' : 'No'; ?>
  • 20. CONTROL AND LOOPING STRUCTURE 1. If/else 2. Switch 3. While 4. For loop 5. Foreach
  • 21. IF/ELSE STATEMENT Here is the example to use if/else statement <?php // change message depending on whether // number is less than zero or not $number = -88; if ($number < 0) { echo 'That number is negative'; } else { echo 'That number is either positive or zero'; } ?>
  • 22. THE IF-ELSEIF-ELSE STATEMENT <?php // handle multiple possibilities if($answer == ‘y’) { print "The answer was yesn"; else if ($answer == ‘n’) { print "The answer was non"; }else{ print "Error: $answer is not a valid answern"; } ?>
  • 23. THE SWITCH-CASE STATEMENT <?php // handle multiple possibilities switch ($answer) { case 'y': print "The answer was yesn"; break; case 'n': print "The answer was non"; break; default: print "Error: $answer is not a valid answern"; break; } ?>
  • 24. WHILE LOOP SYNTAX while (condition): code to be executed; endwhile; EXAMPLE <?php // repeats until counter becomes 10 $counter = 1; while($counter < 10){ echo $counter; $counter++; } ?>
  • 25. THE DO-WHILE LOOP SYNTAX: do { code to be executed; } while (condition); EXAMPLE <?php // repeats until counter becomes 10 $counter = 1; do{ echo $counter; $counter++; }while($counter < 10); ?>
  • 26. THE FOR LOOP SYNTAX for (init; cond; incr) { code to be executed; } init: Is mostly used to set a counter, but can be any code to be executed once at the beginning of the loop statement. cond: Is evaluated at beginning of each loop iteration. If the condition evaluates to TRUE, the loop continues and the code executes. If it evaluates to FALSE, the execution of the loop ends. incr: Is mostly used to increment a counter, but can be any code to be executed at the end of each loop.
  • 27. BREAKING A LOOP Occasionally it is necessary to exit from a loop before it has met whatever completion criteria were specified. To achieve this, the break statement must be used. The following example contains a loop that uses the break statement to exit from the loop when i = 100, even though the loop is designed to iterate 100 times: for ($i = 0; $i < 100; $i++) { if ($i == 10) { break; } }
  • 28. CONTINUE Skipping Statements in Current Loop Iteration. continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. for ($i = 0; $i < 5; ++$i) { if ($i == 2) continue print "$in"; }
  • 29. EXAMPLE: <?php // repeat continuously until counter becomes 10 // output: for ($x=1; $x<10; $x++) { echo "$x "; } ?>
  • 30. ARRAY IN PHP An array can store one or more values in a single variable name. There are three different kind of arrays: 1. Numeric array - An array with a numeric ID key 2. Associative array - An array where each ID key is associated with a value 3. Multidimensional array - An array containing one or more arrays
  • 31. FOREACH LOOP SYNTAX: foreach (array as value) { code to be executed; } <?php $fruits = array( 'a' => 'apple', 'b' => 'banana', 'p' => 'pineapple', 'g' => 'grape'); ?>
  • 32. FOREACH LOOP $array = ['apple', 'banana', 'orange‘]; foreach ($array as $value) { $array = strtoupper($value); } foreach ($array as $key => $value) { $array[$key] = strtolower($value); } var_dump($array); array(3) { [0]=> string(3) "APPLE" [1]=> string(3) "BANANA" [2]=> &string(3) "ORANGE" }
  • 33. ERRORS AND ERROR MANAGEMENT Errors: Basically errors can be of one of two types External Errors Logic Errors (Bugs) What about these error types? External Errors will always occur at some point or another External Errors which are not accounted for are Logic Errors Logic Errors are harder to track down
  • 34. PHP ERRORS Four levels of error condition to start with • Strict standard problems (E_STRICT) • Notices (E_NOTICE) • Warnings (E_WARNING) • Errors (E_ERROR) To enable error in PHP : ini_set('display_errors', 1); error_reporting(E_ALL);
  • 35. PHP ERRORS EXAMPLE // E_NOTICE : <?php echo $x = $y + 3; ?> Notice: Undefined variable: y // E_WARNING <?php $fp = fopen('test_file', 'r'); ?> Warning: fopen(test_file): failed to open stream: No such file or directory // E_ERROR <?php NonFunction(); ?> Fatal error: Call to undefined function NonFunction()
  • 36. REQUIRE, INCLUDE 1. require('filename.php');  Include and evaluate the specified file  Fatal Error 2. include('filename.php');  Include and evaluate the specified file  Warning 3. require_once/include_once  If already included,won't be include again
  • 37. TUTORIAL 1. Write a PHP script using a ‘do while’ loop that accept value from two input box and perform addition, subtraction, multiplication, division,modulus, square-root, square, Factorial operation on two value then display total as the output. Example: First value input field + select box for operator selection + second value = OUTPUT
  • 38. 2.Create a php function that accepts an integer value and other information like name and outputs a message based on the number entered by user in input field. Use a ‘switch’ statement for interger and display values. For a remainder of 0, print: “Welcome ”, [name], For a remainder of 1, print: “How are you,[name]?” For a remainder of 2, print: “I’m doing well, Thank you” For a remainder of 3, print: “Have a nice day” For a remainder of 4, print: “Good-bye”
  • 39. 3. Write a PHP program that display series of numbers (1,2,3,4, 5....etc) in an infinite loop. The program should quit if someone hits a specific ESCAPE key. 4. Create a PHP Program that Use an associative array to assign for person name with their favourite color and print “x favourite color is y” using foreach loop in HTML table
  • 40. 5. Create a PHP program that get system date and convert it in different formats with usingdisplay in Indian Timezone. 1. 29-Jun-2015 2. 06 29 2013 23:05 PM 3. 29th June 2015 4:20:01 4. Tomorrow 5. Get the date of Next week from today 6. Get the date of Next monday 6.Create a Program which accept Year from selectbox and display computed age based on the Selected Value.