SlideShare a Scribd company logo
PHP Basics
Henry Osborne
Syntax
 PHP’s

syntax derived from many languages

 More

Java-like with the latest objectoriented additions

 Designed

CPTR304: Internet Authoring

primarily as a text processor

2
PHP Tags
Standard Tags

<? php
... code
?>

Short Tags

<?
... code
?>
<?=$variable ?>

Script Tags

<script language=“php”
... code
</script>

ASP Tags

<%
... code
%>

CPTR304: Internet Authoring

Short tags, script tags and ASP tags are all considered deprecated.

3
Comments
// Single line comment

# Single line comment

/* Multi-line
comment
*/

/**
* API Documentation Example
*
*
*/
function foo($bar) { }

CPTR304: Internet Authoring

4
Whitespaces


You can’t have any whitespaces between <? and
php



You cannot break apart keywords



You cannot break apart variable names and
function names

CPTR304: Internet Authoring

5
Code Block

{

//Some comments
f(); // a function call
}
CPTR304: Internet Authoring

6
Data Types
boolean

true or false

int

signed numeric integer value

float

signed floating-point value

string

collection of binary data

CPTR304: Internet Authoring

7
Numeric Values
Decimal

10; -11; 1452

Standard decimal notation

Octal

0666, 0100

Octal notation – identified by its
leading zero and used to mainly
express UNIX-style access
permissions

Hexadecimal

0x123; 0XFF; -0x100

Base-16 notation

CPTR304: Internet Authoring

8
Numeric Values, cont’d
Decimal

0.12; 1234.43; -.123

Traditional

Exponential

2E7, 1.2e2

Exponential notation – a set of
significant digits (mantissa),
followed by the case-insensitive
letter E and by an exponent.

CPTR304: Internet Authoring

9
Strings
 Equivalent

to text (according to many
programmers)

 Actually,

an ordered collection of binary

data

CPTR304: Internet Authoring

10
Booleans


Used as the basis for logical operations



Boolean conversion has special rules:
A

number converted to Boolean becomes false if the
original number is zero, and true otherwise

A

string is converted to false only if it is empty or if
it contains the single character 0

 When

converted to a number or string, a Boolean
becomes 1 if true, and 0 otherwise

CPTR304: Internet Authoring

11
Compound Data Types


Arrays are containers of ordered data elements;



Objects are containers of both data and code.

CPTR304: Internet Authoring

12
Other Data Types
 NULL

– variable has no value

– used to indicate external
resources that are not used natively by PHP

 resource

CPTR304: Internet Authoring

13
Type Conversion
$x = 10.88;
echo (int) $x;

CPTR304: Internet Authoring

14
Variables


A variable can contain any type of data



PHP is loosely typed as opposed to being strongly typed
like C, C++, and Java



Identified by a dollar sign $, followed by an identifier
name


$name = „valid‟; //valid identifier



$_name = „valid‟; //valid identifier



$1name = „invalid‟; //invalid identifier, starts
with a number

CPTR304: Internet Authoring

15
Variable Variables


A variable whose name is contained in another variable

$name = „foo‟;
$$name = „bar‟;

echo $foo; //Displays „bar‟

CPTR304: Internet Authoring

16
Variable Variables, cont’d
$name = „123‟;
/* 123 is your variable name, this would normally
be invalid. */
$$name = „456‟;
// Again, you assign a value
echo ${„123‟};
//Finally, using curly braces you can output „456‟
CPTR304: Internet Authoring

17
Variable Variables, cont’d
function myFunc() {
echo „myFunc!‟;

}
$f = „myFunc‟;

$f(); //will call myFunc();

CPTR304: Internet Authoring

18
Determining If a Variable Exists
Use the special construct isset()
echo isset ($x);

CPTR304: Internet Authoring

19
Referencing Variables

$a = 10;

$b = &$a;

//by reference

$b = 20;
echo $a;

CPTR304: Internet Authoring

//Outputs 20

20
Constants
define(„EMAIL‟, „davey.php.net‟); //Valid name
echo EMAIL;
define („USE_XML‟, true);
if(USE_XML) {

}

define („1CONSTANT‟, „some value‟); //Invalid
CPTR304: Internet Authoring

21
Operators


Assignment



Arithmetic



String



Comparison



Logical

CPTR304: Internet Authoring

22
Additional Operators
 Bitwise
 Error

– manipulating bits using Boolean math

Control – error suppression

 Execution

– executing system commands

 Incrementing/Decrementing

 Type

– identifying Objects

CPTR304: Internet Authoring

23
String Concatenation
$string = “foo” . “bar”;
$string2 = “baz”;
$string .= $string2;
echo $string;
CPTR304: Internet Authoring

24
Bitwise Operators
&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

<<

Bitwise left shift

>>

Bitwise right shift

CPTR304: Internet Authoring

25
$x = 1;
echo $x << 1; //Outputs 2
echo $x << 2; //Outputs 4
$x = 8;
echo $x >> 1; //Outputs 4
echo $x >> 2; //Outputs 2
CPTR304: Internet Authoring

26
$x = 1;
echo $x << 32; //Outputs 0

echo $x * pow(2, 32); //Outputs 4,294,967,296

CPTR304: Internet Authoring

27
Comparisons
==

Equivalence – evaluates to true if both operands have the
same value but not necessarily the same type.

===

Identity – evaluates to true only if the operands are of the
same type and contain the same value.

!=

Not-equivalent – evaluates to true if the operands are not
equivalent, without regards to their type.

!==

Not-identical – evaluates to true if the operands are not of
the same type or contain the same value.

CPTR304: Internet Authoring

28
Logical Operators
 &&

/ and
 || / or
 XOR

CPTR304: Internet Authoring

29
Error Suppression Operator
$x = @mysql_connect();

CPTR304: Internet Authoring

30
Conditional Structures


Decision-making





if-then-else

switch

Iteration


while()



do...while()



for

CPTR304: Internet Authoring

31
if-then-else
if (expression1) {
} elseif (expression2){

} else {
}
CPTR304: Internet Authoring

32
Ternary Operator
echo 10 == $x ? „Yes‟ : „No‟;

if (10
echo
} else
echo
}
CPTR304: Internet Authoring

== $x){
„Yes‟;
{
„No‟;
33
Switch
$a = 0;
switch ($a) {

case true:
break;
case 0:
break;
default:
break;
}
CPTR304: Internet Authoring

34
while()
$i = 0;
while ($i < 10) {
echo $i . PHP_EOL;
$i++;

}

CPTR304: Internet Authoring

35
do…while()
$i = 0;
do {
echo $i . PHP_EOL;
$i++;

} while ($i < 10)

CPTR304: Internet Authoring

36
for()
for ($i = 0; $i < 10; $i++) {
echo $i . PHP_EOL;
}

CPTR304: Internet Authoring

37
Errors and Error Management
Compile-time errors Detected by parser while compiling a script.
Cannot be trapped within the script itself.
Fatal errors

Halt the execution of a script. Cannot be trapped.

Recoverable errors

Represent significant failures but can be handled
in a safe way.

Warnings

Recoverable errors that indicate a run-time fault.
Does not halt execution.

Notices

Indicates that an error condition has occurred but
is not necessarily significant. Does not halt script
execution.
38

CPTR304: Internet Authoring
PHP Basics

More Related Content

PDF
Zend Certification Preparation Tutorial
PDF
lab4_php
PPT
Class 2 - Introduction to PHP
PDF
Functions in PHP
ODP
PHP Web Programming
PPTX
PHP Powerpoint -- Teach PHP with this
PPT
02 Php Vars Op Control Etc
PPT
Class 3 - PHP Functions
Zend Certification Preparation Tutorial
lab4_php
Class 2 - Introduction to PHP
Functions in PHP
PHP Web Programming
PHP Powerpoint -- Teach PHP with this
02 Php Vars Op Control Etc
Class 3 - PHP Functions

What's hot (19)

PDF
Php Tutorials for Beginners
PDF
Operators in PHP
PPT
PHP Workshop Notes
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
ODP
Php Learning show
PDF
03phpbldgblock
PDF
Practice exam php
PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
PPSX
Php using variables-operators
ODP
The promise of asynchronous PHP
PPT
Php basics
ODP
PHP Tips for certification - OdW13
PPT
php 2 Function creating, calling, PHP built-in function
PPT
Php Operators N Controllers
PPT
Class 5 - PHP Strings
PPTX
Php string function
PDF
Zend Certification PHP 5 Sample Questions
PPTX
Operators php
PPTX
Php basics
Php Tutorials for Beginners
Operators in PHP
PHP Workshop Notes
PHP - DataType,Variable,Constant,Operators,Array,Include and require
Php Learning show
03phpbldgblock
Practice exam php
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php using variables-operators
The promise of asynchronous PHP
Php basics
PHP Tips for certification - OdW13
php 2 Function creating, calling, PHP built-in function
Php Operators N Controllers
Class 5 - PHP Strings
Php string function
Zend Certification PHP 5 Sample Questions
Operators php
Php basics
Ad

Viewers also liked (7)

PPT
Presentació_ClockingIT
PPTX
Website Security
PPTX
Creative Thinking
PPTX
Establishing a Web Presence
PPTX
Getting started with Android Programming
PPTX
Social Media and You
PPTX
Cryptography
Presentació_ClockingIT
Website Security
Creative Thinking
Establishing a Web Presence
Getting started with Android Programming
Social Media and You
Cryptography
Ad

Similar to PHP Basics (20)

PPTX
PHP Basics
PPT
Web Technology_10.ppt
PPTX
06-PHPIntroductionserversicebasicss.pptx
PPTX
Php intro by sami kz
PPT
P H P Part I, By Kian
PPT
Synapse india complain sharing info about php chaptr 26
PPT
Php Chapter 1 Training
PPT
PHP Scripting
PDF
PHP Reviewer
PPT
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
PPT
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
PPT
Php i basic chapter 3
PPTX
Learn PHP Basics
PDF
php AND MYSQL _ppt.pdf
PPT
Internet Technology and its Applications
PPTX
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PHP Basics
Web Technology_10.ppt
06-PHPIntroductionserversicebasicss.pptx
Php intro by sami kz
P H P Part I, By Kian
Synapse india complain sharing info about php chaptr 26
Php Chapter 1 Training
PHP Scripting
PHP Reviewer
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3
Learn PHP Basics
php AND MYSQL _ppt.pdf
Internet Technology and its Applications
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx

More from Henry Osborne (20)

PPTX
Android Fundamentals
PPTX
Open Source Education
PPTX
Security Concepts - Linux
PPTX
Networking Basics with Linux
PPTX
Disk and File System Management in Linux
PPTX
Drawing with the HTML5 Canvas
PPTX
HTML5 Multimedia Support
PPTX
Information Architecture
PPTX
Interface Design
PPTX
Universal Usability
PPTX
XML and Web Services
PPTX
Elements of Object-oriented Design
PPTX
Database Programming
PPTX
OOP in PHP
PPTX
Web Programming
PPTX
PHP Strings and Patterns
PPTX
PHP Functions & Arrays
PPTX
Activities, Fragments, and Events
PPTX
Web Programming and Internet Technologies
PPTX
Angels & Demons
Android Fundamentals
Open Source Education
Security Concepts - Linux
Networking Basics with Linux
Disk and File System Management in Linux
Drawing with the HTML5 Canvas
HTML5 Multimedia Support
Information Architecture
Interface Design
Universal Usability
XML and Web Services
Elements of Object-oriented Design
Database Programming
OOP in PHP
Web Programming
PHP Strings and Patterns
PHP Functions & Arrays
Activities, Fragments, and Events
Web Programming and Internet Technologies
Angels & Demons

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
master seminar digital applications in india
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Institutional Correction lecture only . . .
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
master seminar digital applications in india
TR - Agricultural Crops Production NC III.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Renaissance Architecture: A Journey from Faith to Humanism
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Institutional Correction lecture only . . .
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Week 4 Term 3 Study Techniques revisited.pptx
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

PHP Basics

  • 2. Syntax  PHP’s syntax derived from many languages  More Java-like with the latest objectoriented additions  Designed CPTR304: Internet Authoring primarily as a text processor 2
  • 3. PHP Tags Standard Tags <? php ... code ?> Short Tags <? ... code ?> <?=$variable ?> Script Tags <script language=“php” ... code </script> ASP Tags <% ... code %> CPTR304: Internet Authoring Short tags, script tags and ASP tags are all considered deprecated. 3
  • 4. Comments // Single line comment # Single line comment /* Multi-line comment */ /** * API Documentation Example * * */ function foo($bar) { } CPTR304: Internet Authoring 4
  • 5. Whitespaces  You can’t have any whitespaces between <? and php  You cannot break apart keywords  You cannot break apart variable names and function names CPTR304: Internet Authoring 5
  • 6. Code Block { //Some comments f(); // a function call } CPTR304: Internet Authoring 6
  • 7. Data Types boolean true or false int signed numeric integer value float signed floating-point value string collection of binary data CPTR304: Internet Authoring 7
  • 8. Numeric Values Decimal 10; -11; 1452 Standard decimal notation Octal 0666, 0100 Octal notation – identified by its leading zero and used to mainly express UNIX-style access permissions Hexadecimal 0x123; 0XFF; -0x100 Base-16 notation CPTR304: Internet Authoring 8
  • 9. Numeric Values, cont’d Decimal 0.12; 1234.43; -.123 Traditional Exponential 2E7, 1.2e2 Exponential notation – a set of significant digits (mantissa), followed by the case-insensitive letter E and by an exponent. CPTR304: Internet Authoring 9
  • 10. Strings  Equivalent to text (according to many programmers)  Actually, an ordered collection of binary data CPTR304: Internet Authoring 10
  • 11. Booleans  Used as the basis for logical operations  Boolean conversion has special rules: A number converted to Boolean becomes false if the original number is zero, and true otherwise A string is converted to false only if it is empty or if it contains the single character 0  When converted to a number or string, a Boolean becomes 1 if true, and 0 otherwise CPTR304: Internet Authoring 11
  • 12. Compound Data Types  Arrays are containers of ordered data elements;  Objects are containers of both data and code. CPTR304: Internet Authoring 12
  • 13. Other Data Types  NULL – variable has no value – used to indicate external resources that are not used natively by PHP  resource CPTR304: Internet Authoring 13
  • 14. Type Conversion $x = 10.88; echo (int) $x; CPTR304: Internet Authoring 14
  • 15. Variables  A variable can contain any type of data  PHP is loosely typed as opposed to being strongly typed like C, C++, and Java  Identified by a dollar sign $, followed by an identifier name  $name = „valid‟; //valid identifier  $_name = „valid‟; //valid identifier  $1name = „invalid‟; //invalid identifier, starts with a number CPTR304: Internet Authoring 15
  • 16. Variable Variables  A variable whose name is contained in another variable $name = „foo‟; $$name = „bar‟; echo $foo; //Displays „bar‟ CPTR304: Internet Authoring 16
  • 17. Variable Variables, cont’d $name = „123‟; /* 123 is your variable name, this would normally be invalid. */ $$name = „456‟; // Again, you assign a value echo ${„123‟}; //Finally, using curly braces you can output „456‟ CPTR304: Internet Authoring 17
  • 18. Variable Variables, cont’d function myFunc() { echo „myFunc!‟; } $f = „myFunc‟; $f(); //will call myFunc(); CPTR304: Internet Authoring 18
  • 19. Determining If a Variable Exists Use the special construct isset() echo isset ($x); CPTR304: Internet Authoring 19
  • 20. Referencing Variables $a = 10; $b = &$a; //by reference $b = 20; echo $a; CPTR304: Internet Authoring //Outputs 20 20
  • 21. Constants define(„EMAIL‟, „davey.php.net‟); //Valid name echo EMAIL; define („USE_XML‟, true); if(USE_XML) { } define („1CONSTANT‟, „some value‟); //Invalid CPTR304: Internet Authoring 21
  • 23. Additional Operators  Bitwise  Error – manipulating bits using Boolean math Control – error suppression  Execution – executing system commands  Incrementing/Decrementing  Type – identifying Objects CPTR304: Internet Authoring 23
  • 24. String Concatenation $string = “foo” . “bar”; $string2 = “baz”; $string .= $string2; echo $string; CPTR304: Internet Authoring 24
  • 25. Bitwise Operators & Bitwise AND | Bitwise OR ^ Bitwise XOR << Bitwise left shift >> Bitwise right shift CPTR304: Internet Authoring 25
  • 26. $x = 1; echo $x << 1; //Outputs 2 echo $x << 2; //Outputs 4 $x = 8; echo $x >> 1; //Outputs 4 echo $x >> 2; //Outputs 2 CPTR304: Internet Authoring 26
  • 27. $x = 1; echo $x << 32; //Outputs 0 echo $x * pow(2, 32); //Outputs 4,294,967,296 CPTR304: Internet Authoring 27
  • 28. Comparisons == Equivalence – evaluates to true if both operands have the same value but not necessarily the same type. === Identity – evaluates to true only if the operands are of the same type and contain the same value. != Not-equivalent – evaluates to true if the operands are not equivalent, without regards to their type. !== Not-identical – evaluates to true if the operands are not of the same type or contain the same value. CPTR304: Internet Authoring 28
  • 29. Logical Operators  && / and  || / or  XOR CPTR304: Internet Authoring 29
  • 30. Error Suppression Operator $x = @mysql_connect(); CPTR304: Internet Authoring 30
  • 32. if-then-else if (expression1) { } elseif (expression2){ } else { } CPTR304: Internet Authoring 32
  • 33. Ternary Operator echo 10 == $x ? „Yes‟ : „No‟; if (10 echo } else echo } CPTR304: Internet Authoring == $x){ „Yes‟; { „No‟; 33
  • 34. Switch $a = 0; switch ($a) { case true: break; case 0: break; default: break; } CPTR304: Internet Authoring 34
  • 35. while() $i = 0; while ($i < 10) { echo $i . PHP_EOL; $i++; } CPTR304: Internet Authoring 35
  • 36. do…while() $i = 0; do { echo $i . PHP_EOL; $i++; } while ($i < 10) CPTR304: Internet Authoring 36
  • 37. for() for ($i = 0; $i < 10; $i++) { echo $i . PHP_EOL; } CPTR304: Internet Authoring 37
  • 38. Errors and Error Management Compile-time errors Detected by parser while compiling a script. Cannot be trapped within the script itself. Fatal errors Halt the execution of a script. Cannot be trapped. Recoverable errors Represent significant failures but can be handled in a safe way. Warnings Recoverable errors that indicate a run-time fault. Does not halt execution. Notices Indicates that an error condition has occurred but is not necessarily significant. Does not halt script execution. 38 CPTR304: Internet Authoring

Editor's Notes

  • #3: PHP was built with the intent of providing simplicity and choicePHP derived predominantly from C with influence from PerlPHP code can be inserted directly into a text file using a special set of tags
  • #4: Standard tags – de-facto opening and closing tags; best solution for portability and backward compatibilityShort tags – conflicts with XML headersScript tags – allowed HTML editors, unable to cope with PHP tags, to ignore the PHP code
  • #7: A series of statements enclosed between two bracesCode blocks can be nested
  • #8: Two categories: scalar and compositeScalar contains only one value at a time
  • #9: PHP recognizes two types of numbers, integers and floating-point valuesOctal numbers can be easily confused with decimal numbers
  • #10: Floating-point numbers, also called floats and, sometimes, doublesExponent: the resulting number is expressed multiplied by 10 to the power of the exponent – e.g. 1e2 = 10064-bit platforms may be capable of representing a wider range of integer numbers than 32-bit
  • #11: String could also be the contents of an image file, a spreadsheet, or even a music recording
  • #13: PHP supports two compound data types; they are essentially containers of other data
  • #15: NB: A value cannot be converted to some special types e.g. you cannot convert any value to a resource – you can, however, convert a resource to a numeric or string data type, in which case PHP will return the numeric ID of the resource
  • #16: Loosely typed: type of variable implicitly changed as neededStrongly typed: variables can only contain one type of data
  • #17: $$name – special syntax used to indicate to the interpreter to use the contents of $name to reference a new variable
  • #18: Use with extreme careThey make code difficult to understand and documentImproper use can lead to significant security issues
  • #19: Variables can be used to hold function names
  • #20: A call to isset() will return true if a variable exists or has a value other than NULL
  • #31: Causes PHP to ignore almost all error messages that occur while that expression is being evaluated