php variables

8
PHP Variables

Upload: ritwik-das

Post on 28-Jun-2015

236 views

Category:

Education


0 download

DESCRIPTION

This presentation shows fundamentals of PHP Variables - declaring variables, Variable scopeStatic Variable and Reserved words.

TRANSCRIPT

Page 1: Php variables

PHP Variables

Page 2: Php variables

TOC

• Declaring variables

• Variable scope

• Static Variable

• Reserved words

Page 3: Php variables

What is a variable

• Stores value• myName is “Joe” is $myName = “Joe”;

//strings• UserId is 086AFTG is $UserId = 086AFTG; //

numbers

Page 4: Php variables

Rules of PHP Variable naming

• starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores.

• variable name is case-sensitive. So $MyName and $MYNAME are not same.

• You don’t need to mention what kind of value you are going to store.

Page 5: Php variables

Variable Scopes

• Local Scope function myFunction ()

{$FirstName = “Joe”; // local

//do something}

• Global Scope $department = “Marketing”; // global function myFunction ()

{$FirstName = “w3resource”; 

//do something}

Page 6: Php variables

Static Variables

function test_count() { static $x=1; echo $x; $x++; } test_count(); test_count(); test_count(); //Displays 123

Page 7: Php variables

Reserved Words

• Cannot be used as constants, class names, function or method names.

• May be used as variable names, but confusing so avoidable.

• Examples: case, namespace, __METHOD__ etc.