SlideShare uma empresa Scribd logo
1 de 39
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

Mais conteúdo relacionado

Mais procurados

Php basics
Php basicsPhp basics
Php basics
hamfu
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
julien pauli
 
Php Operators N Controllers
Php Operators N ControllersPhp Operators N Controllers
Php Operators N Controllers
mussawir20
 

Mais procurados (19)

Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Practice exam php
Practice exam phpPractice exam php
Practice exam php
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
Php basics
Php basicsPhp basics
Php basics
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
 
Php Operators N Controllers
Php Operators N ControllersPhp Operators N Controllers
Php Operators N Controllers
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Php string function
Php string function Php string function
Php string function
 
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample Questions
 
Operators php
Operators phpOperators php
Operators php
 
Php basics
Php basicsPhp basics
Php basics
 

Destaque (7)

Presentació_ClockingIT
Presentació_ClockingITPresentació_ClockingIT
Presentació_ClockingIT
 
Website Security
Website SecurityWebsite Security
Website Security
 
Creative Thinking
Creative ThinkingCreative Thinking
Creative Thinking
 
Establishing a Web Presence
Establishing a Web PresenceEstablishing a Web Presence
Establishing a Web Presence
 
Getting started with Android Programming
Getting started with Android ProgrammingGetting started with Android Programming
Getting started with Android Programming
 
Social Media and You
Social Media and YouSocial Media and You
Social Media and You
 
Cryptography
CryptographyCryptography
Cryptography
 

Semelhante a PHP Basics

06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx
20521742
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
phelios
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 

Semelhante a PHP Basics (20)

PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
 
06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
 
Php 26
Php 26Php 26
Php 26
 
PHP Scripting
PHP ScriptingPHP Scripting
PHP Scripting
 
PHP Reviewer
PHP ReviewerPHP Reviewer
PHP Reviewer
 
Php
PhpPhp
Php
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
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 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php 26
Php 26Php 26
Php 26
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Internet Technology and its Applications
Internet Technology and its ApplicationsInternet Technology and its Applications
Internet Technology and its Applications
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 

Mais de Henry Osborne

Mais de Henry Osborne (20)

Android Fundamentals
Android FundamentalsAndroid Fundamentals
Android Fundamentals
 
Open Source Education
Open Source EducationOpen Source Education
Open Source Education
 
Security Concepts - Linux
Security Concepts - LinuxSecurity Concepts - Linux
Security Concepts - Linux
 
Networking Basics with Linux
Networking Basics with LinuxNetworking Basics with Linux
Networking Basics with Linux
 
Disk and File System Management in Linux
Disk and File System Management in LinuxDisk and File System Management in Linux
Disk and File System Management in Linux
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
 
HTML5 Multimedia Support
HTML5 Multimedia SupportHTML5 Multimedia Support
HTML5 Multimedia Support
 
Information Architecture
Information ArchitectureInformation Architecture
Information Architecture
 
Interface Design
Interface DesignInterface Design
Interface Design
 
Universal Usability
Universal UsabilityUniversal Usability
Universal Usability
 
XML and Web Services
XML and Web ServicesXML and Web Services
XML and Web Services
 
Elements of Object-oriented Design
Elements of Object-oriented DesignElements of Object-oriented Design
Elements of Object-oriented Design
 
Database Programming
Database ProgrammingDatabase Programming
Database Programming
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Web Programming
Web ProgrammingWeb Programming
Web Programming
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
 
Web Programming and Internet Technologies
Web Programming and Internet TechnologiesWeb Programming and Internet Technologies
Web Programming and Internet Technologies
 
Angels & Demons
Angels & DemonsAngels & Demons
Angels & Demons
 

Último

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

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

Notas do Editor

  1. 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
  2. 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
  3. A series of statements enclosed between two bracesCode blocks can be nested
  4. Two categories: scalar and compositeScalar contains only one value at a time
  5. PHP recognizes two types of numbers, integers and floating-point valuesOctal numbers can be easily confused with decimal numbers
  6. 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
  7. String could also be the contents of an image file, a spreadsheet, or even a music recording
  8. PHP supports two compound data types; they are essentially containers of other data
  9. 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
  10. Loosely typed: type of variable implicitly changed as neededStrongly typed: variables can only contain one type of data
  11. $$name – special syntax used to indicate to the interpreter to use the contents of $name to reference a new variable
  12. Use with extreme careThey make code difficult to understand and documentImproper use can lead to significant security issues
  13. Variables can be used to hold function names
  14. A call to isset() will return true if a variable exists or has a value other than NULL
  15. Causes PHP to ignore almost all error messages that occur while that expression is being evaluated