0% found this document useful (0 votes)
8 views33 pages

Working With Variables

Uploaded by

dallawais543
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
8 views33 pages

Working With Variables

Uploaded by

dallawais543
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 33

Working With Variables

VARIABLES

• If you are writing any program in any programming


language you have to keep track of pieces of
information.
• if you are building a loan calculator, you need to keep
track of the amount of the loan, the number of months ,
the interest rate etc.
• if you are writing a game program you might need to
know the current score, the position of player on screen,
how many lives do you have, what image do you use for
player,
• this is all data and we create variable to hold that data.
VARIABLES

• Variables are what really give PHP its power.


• Variables are "containers" for storing information.
• Variables allow you to store data and recall it later.
• Variables in PHP are quite different from compiled
languages such as C and Java. This is because their
weakly typed nature.
• You don’t need to declare variables before using
them.
• You don’t need to declare their type.
rak@gmail.com 01/01/2000 RAK 32000

email customerDOB name highScore


RULES FOR CREATING VAERIABLES

• A variable must start with a dollar ($) sign, followed by the


variable name.
• It can only contain alpha-numeric character and underscore (A-
z, 0-9, _).
• A variable name must start with a letter or underscore (_)
character.
• A PHP variable name cannot contain spaces.
• One thing to be kept in mind that the variable name cannot
start with a number or special symbols.
• PHP variables are case-sensitive, so $name and $NAME both
are treated as different variable.
CREATING VAERIABLES

All variables in PHP start with a $ sign symbol. Variables


may contain strings, numbers, or arrays.
$year;
$customerEmail;
letters
$todaysDate; numbers
$first_name; _
$x;
$_day; $99problems
$_2345; $problems99
CREATING VAERIABLES

$year; 2017
NULL
$year = 2017; year
VARIABLE NAMES ARE CASE SENSITIVE

$x = 200; 200
x

$X = 200; 200
X
WEAK TYPING

$myVariable;

false
Hello
200
NULL
true
myVariable
$myVariable = 200;
$myVariable = “Hello”;
$myVariable = ‘Hello’;
$myVariable = true;
$myVariable = false;
STRINGS

echo “Hello, World!”;

$message = “Hello, World!”

echo $message;
WORKING WITH STRINGS

$myString = “Double quotes work.”;

$myString = ‘Single quotes work too.’;

$myString = “ One or other. ’;


QUOTES INSIDE QUOTES

$phrase= ‘ Don’t mix your quotes.’;

$phrase = “ Don’t mix your quotes.”;

$phrase = “He said “that’s fine,” and left.”;

$phrase = “He said \“that’s fine,\” and left.”;


Constant In PHP
• <?php
• define("NAME", "Welcome to Hidaya!");
• echo NAME;
• ?>
Boolean
String
Scalar
Integer
Data Types
Float/double

Array
Compound
Object

Null
Special
Resources
Type
integer Double/float String Boolean

Example
5 10.56 “php” True/false

Description
Integers are the simplest type Strings are any combination A boolean expresses a truth
of data. They are simply Floating point numbers of letters, numbers, spaces value. It can be
numbers. (Whole numbers) and other characters either TRUE or FALSE.
Type
Array Object Null Resources

Example
Array(1.2.3) $bar = new foo; Id#2

Description
An array is a multitude
A resources is a special
of elements that have The special NULL value
collection of variables data type, holding a
been grouped together represents a variable
& functions reference to an
within a single PHP with no value
external resource
variable.
USING OPERATORS

 Unary
 Unary operators act on one operand
 ++ -- !
 Binary
 Binary operators are used on two operands:
 Arithmetic + - * / %
 Arithmetic Assignment += -= *= /=
 Assignment =
 Comparison == === != !== < >
<= >=
 Logical and or xor ! && ||
 Ternary
 ?:
ASSIGNMENT OPERATOR

$balance = 500;
ARTHIMETIC OPERATORS

+ - * /
$a = 100;
$b = 50;
$result = $a +-/* $b;
150
2
5000
50
Comparison Operators
OPERATOR PRECEDENCE

$result = 5 + 5 * 10;
55 5 + 50
OPERATOR PRECEDENCE

$result = (5 + 5) * 10;
100 10 * 10
ARTHIMETIC OPERATORS

$score = $score + 10;


$score += 10;
+= -= *= /=
INCREMENT / DECREMENT

$a = $a + 1; $a = $a - 1;
$a += 1; $a -= 1;
$a++; $a--;
++$a; --$a;
Concatenation Operator (.)
• The concatenation operator concatenates two
strings. This operator works only on strings; thus,
any non-string operand is first converted to one.
• The following example would print out “welcome
to Hidaya" :
• <?php
• $name = “Hidaya”;
• echo " welcome to" . $name;
• ?>
PHP Functions
• settype() - Set the type of a variable
• gettype() - Get the type of a variable
• isset() — Determine if a variable is set and is not NULL
• is_array() - Finds whether a variable is an array
• is_bool() - Finds out whether a variable is a boolean
• is_float() - Finds whether the type of a variable is float
• is_int() - Find whether the type of a variable is integer
• is_null() - Finds whether a variable is NULL
• is_numeric() - Finds whether a variable is a number or a numeric string
• is_object() - Finds whether a variable is an object
• is_resource() - Finds whether a variable is a resource
• is_scalar() - Finds whether a variable is a scalar
• is_string() - Find whether the type of a variable is string
• empty() - Determine whether a variable is empty
• unset() - Unset a given variable
PHP Functions
• gettype — Get the type of a variable
• Returns the type of the PHP variable.
• <?php

$data = ”hello”;

echo gettype($data); // string

?>
PHP Functions
• settype — Set the type of a variable
• Returns TRUE on success or FALSE on failure.
• <?php

$data = 10.55;
// convert the type of $data
echo gettype($data);
echo “<br>”;
echo $data;
settype($data,”integer”);
echo “<br>”;
echo gettype($data);
echo “<br>”;
echo $data;

?>
Comments in PHP
• As with most other scripting languages, PHP has a way to make
comments in your code.
• Comments can be used to create notes or to “hide” lines of
code without deleting them. Anything within a commented
section will not be processed by the server as PHP. There are
several ways to create comments in PHP, but the two most
common are:
• A double slash (//) or hash sign (#) will comment out
everything that follows it on that particular line.
• A slash and an asterisk (/*) will comment out everything in the
PHP across-multiple lines until it hits the closing comment tag
of an asterisk and a slash (*/).
Comments in PHP
• <?php
• // single line comment
• # single line comment

• /*
• multiple line
• comment
• */
• ?>
Null Coalesce Operator (??)
• <?php
• $name = "Hidaya";
• echo isset($name)?$name:"not set";
• echo "<br />";
• echo $name ?? "not set";
• ?>
Spaceship Operator (<=>)
• The spaceship operator is used for comparing
two expressions. It returns -1, 0 or 1 when $a is
respectively less than, equal to, or greater than
$b.
Spaceship Operator (<=>)
• // Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

You might also like