Working With Variables-[LAA] PHP Lj.pptx

shahid41103 6 views 32 slides Jun 06, 2024
Slide 1
Slide 1 of 32
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32

About This Presentation

PHP software


Slide Content

Working With Variables By: Laraib Atta Awan (LAA) Program Manager Hidaya (HIST-JHDO)

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. Y ou don’t need to declare their type. VARIABLES

email [email protected] customerDOB 01/01/2000 name RAK highScore 3 2000

CREATING VAERIABLES All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or arrays . $year; $ customerEmail ; $ todaysDate ; $ first_name ; $x; $_day; $_2345; letters numbers _ $99problems $problems99

CREATING VAERIABLES $year; $year = 2017; NULL year 2017

VARIABLE NAMES ARE CASE SENSITIVE $x = 200; $X = 200; x 200 X 200

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

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; define("NAME", "Welcome to Hidaya !",true); echo name; ?>

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 = 500; $balance

ARTHIMETIC OPERATORS + - * / $a = 100; $b = 50; $a $b; = $result 150 + - 50 * 5000 / 2

Comparison Operators

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

OPERATOR PRECEDENCE $result = (5 + 5) * 10; * 10 10 100

ARTHIMETIC OPERATORS $score = $score + 10; $score += 10; += -= *= /=

INCREMENT / DECREMENT $a = $a + 1; $a += 1; $a++; $a = $a - 1; $a -= 1; $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
Tags