php basics

39
PHP Basics Henry Osborne

Upload: henry-osborne

Post on 24-May-2015

610 views

Category:

Education


1 download

DESCRIPTION

A quick introduction to the PHP server-side scripting language

TRANSCRIPT

Page 1: PHP Basics

PHP BasicsHenry Osborne

Page 2: PHP Basics

CPTR304: Internet Authoring 2

Syntax

PHP’s syntax derived from many languages

More Java-like with the latest object-oriented additions

Designed primarily as a text processor

Page 3: PHP Basics

CPTR304: Internet Authoring 3

PHP Tags

Standard Tags <? php... code?>

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

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

ASP Tags <%... code%>

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

Page 4: PHP Basics

CPTR304: Internet Authoring 4

Comments

// Single line comment # Single line comment

/* Multi-linecomment*/

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

Page 5: PHP Basics

CPTR304: Internet Authoring 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

Page 6: PHP Basics

CPTR304: Internet Authoring 6

Code Block

{

//Some comments

f(); // a function call

}

Page 7: PHP Basics

CPTR304: Internet Authoring 7

Data Types

boolean true or false

int signed numeric integer value

float signed floating-point value

string collection of binary data

Page 8: PHP Basics

CPTR304: Internet Authoring 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

Page 9: PHP Basics

CPTR304: Internet Authoring 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.

Page 10: PHP Basics

CPTR304: Internet Authoring 10

Strings

Equivalent to text (according to many programmers)

Actually, an ordered collection of binary data

Page 11: PHP Basics

CPTR304: Internet Authoring 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

Page 12: PHP Basics

CPTR304: Internet Authoring 12

Compound Data Types

Arrays are containers of ordered data elements;

Objects are containers of both data and code.

Page 13: PHP Basics

CPTR304: Internet Authoring 13

Other Data Types

NULL – variable has no value

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

Page 14: PHP Basics

CPTR304: Internet Authoring 14

Type Conversion

$x = 10.88;

echo (int) $x;

Page 15: PHP Basics

CPTR304: Internet Authoring 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

Page 16: PHP Basics

CPTR304: Internet Authoring 16

Variable Variables

A variable whose name is contained in another variable

$name = ‘foo’;

$$name = ‘bar’;

echo $foo; //Displays ‘bar’

Page 17: PHP Basics

CPTR304: Internet Authoring 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’

Page 18: PHP Basics

CPTR304: Internet Authoring 18

Variable Variables, cont’d

function myFunc() {

echo ‘myFunc!’;

}

$f = ‘myFunc’;

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

Page 19: PHP Basics

CPTR304: Internet Authoring 19

Determining If a Variable Exists

Use the special construct isset()

echo isset ($x);

Page 20: PHP Basics

CPTR304: Internet Authoring 20

Referencing Variables

$a = 10;

$b = &$a; //by reference

$b = 20;

echo $a; //Outputs 20

Page 21: PHP Basics

CPTR304: Internet Authoring 21

Constants

define(‘EMAIL’, ‘davey.php.net’); //Valid name

echo EMAIL;

define (‘USE_XML’, true);

if(USE_XML) { }

define (‘1CONSTANT’, ‘some value’); //Invalid

Page 22: PHP Basics

CPTR304: Internet Authoring 22

Operators

Assignment Arithmetic String Comparison Logical

Page 23: PHP Basics

CPTR304: Internet Authoring 23

Additional Operators

Bitwise – manipulating bits using Boolean math

Error Control – error suppressionExecution – executing system commandsIncrementing/DecrementingType – identifying Objects

Page 24: PHP Basics

CPTR304: Internet Authoring 24

String Concatenation

$string = “foo” . “bar”;

$string2 = “baz”;

$string .= $string2;

echo $string;

Page 25: PHP Basics

CPTR304: Internet Authoring 25

Bitwise Operators

& Bitwise AND

| Bitwise OR

^ Bitwise XOR

<< Bitwise left shift

>> Bitwise right shift

Page 26: PHP Basics

CPTR304: Internet Authoring 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

Page 27: PHP Basics

CPTR304: Internet Authoring 27

$x = 1;

echo $x << 32; //Outputs 0

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

Page 28: PHP Basics

CPTR304: Internet Authoring 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.

Page 29: PHP Basics

CPTR304: Internet Authoring 29

Logical Operators

&& / and|| / orXOR

Page 30: PHP Basics

CPTR304: Internet Authoring 30

Error Suppression Operator

$x = @mysql_connect();

Page 31: PHP Basics

CPTR304: Internet Authoring 31

Conditional Structures

Decision-making

if-then-else

switch

Iteration

while()

do...while()

for

Page 32: PHP Basics

CPTR304: Internet Authoring 32

if-then-else

if (expression1) {

} elseif (expression2){

} else {

}

Page 33: PHP Basics

CPTR304: Internet Authoring 33

Ternary Operator

echo 10 == $x ? ‘Yes’ : ‘No’;

if (10 == $x){

echo ‘Yes’;

} else {

echo ‘No’;

}

Page 34: PHP Basics

CPTR304: Internet Authoring 34

Switch

$a = 0;

switch ($a) {

case true:

break;

case 0:

break;

default:

break;

}

Page 35: PHP Basics

CPTR304: Internet Authoring 35

while()

$i = 0;

while ($i < 10) {

echo $i . PHP_EOL;

$i++;

}

Page 36: PHP Basics

CPTR304: Internet Authoring 36

do…while()

$i = 0;

do {

echo $i . PHP_EOL;

$i++;

} while ($i < 10)

Page 37: PHP Basics

CPTR304: Internet Authoring 37

for()

for ($i = 0; $i < 10; $i++) {

echo $i . PHP_EOL;

}

Page 38: PHP Basics

CPTR304: Internet Authoring 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.

Page 39: PHP Basics

PHP Basics