Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables Variables are used to store data or information.

Similar presentations


Presentation on theme: "Variables Variables are used to store data or information."— Presentation transcript:

1 Variables Variables are used to store data or information.
Think of variables as containers that you can put things in. The data stored in a variable is called its value. The value of a variable can be changed. In php (and other programming languages) we often need a place to store values which can later be used by a script or program.

2 Naming Variables Variables always start with $
Var name cannot start with a number - Start Var name with a letter or underscore, then use any alphanumeric character $foo1 or $_foo1 NOT $1foo Case sensitive $foo is different than $Foo Use names that are descriptive, such as count or speed, not c or s.

3 Assigning Variables Use the assignment operator =
$name = "Rasheed"; // quotes for text or string variables $age = 23; // no quotes for numbers echo "$name is $age"; // Rasheed is 23 echo '$name is $age'; // $name is $age

4 Example <?php $baskets = 6; $eggsInBasket = 10;
$total = $baskets * $eggsInBasket; echo "There are $total eggs"; ?> // There are 60 eggs

5 Example <?php $a = "eggs"; $b = "doughnuts"; $a = $b; echo $a;
?> // doughnuts

6 Variable Types String – A string is any group of characters, such
as "The quick brown fox jumped over the lazy dog" or "five" or "5" or "Barack Obama" or "aacd". Strings are enclosed in quotation (" ") marks. $animal = "brown fox"; $name = "Rasheed";

7 Variable Types Integer – Integers are whole numbers, positive or negative, such as 5, -5, 100, 323 $dogs = 10; // Integer $dogs = "10"; // String $foo = ; // 15 $pets = $dogs + 5; // 15

8 Variable Types php is a loosely-typed language, meaning the variable type doesn’t have to be declared. php can decide the type for you, depending on the context, e.g. $dogs = "10"; // string value $animals = 5 + $dogs; // 15 (evaluates as integer)

9 Variable Types Floats – Floats hold fractional numbers, such as 4.2 or Warning – floating point calculations may not always be accurate, because the binary, base 2 program calculator cannot accurately represent some fractional numbers Avoid comparing float numbers if possible

10 Variable Types Boolean – Two possible values, true or false $foo = true;

11 Variable Types Arrays Null Objects Resources We’ll say more about these types later.

12 isset() function The built-in isset() function returns true if a variable has been set, false if not $foo = 33; if (isset($foo)) { echo '$foo is set'; } // $foo is set

13 Constants A constant is a special kind of variable that never changes. Use the define() function with two parameters: define("secondsPerMinute", 60); echo secondsPerMinute; // 60

14 Expressions An expression is anything that has a value. $a = 5 function foo() { return 5; //returns a value of 5 }


Download ppt "Variables Variables are used to store data or information."

Similar presentations


Ads by Google