Php using variables-operators

21
PHP Basic Khem Puthea p utheakhemdeveloper @gmail.com

Transcript of Php using variables-operators

Page 2: Php using variables-operators

Part I: Understanding PHP Basics

Using Variables and Operators

PHP

Prepared by KhmerCourse

Page 3: Php using variables-operators

Storing Data in Variables

៚ Some simple rules for naming variables

៙ Be preceded with a dollar symbol $

៙ And begin with a letter or underscore _

៙ Optionally followed by more letters, numbers, or underscore

៙ Be not permitted

ៜ Punctuation: commas ,, quotation marks ?, or periods .

ៜ Spaces

៙ e.g.

ៜ $id, $_name and $query3: valid

ៜ $96, $day. and email: invalid

៚ Variable names are case-sensitive.

៙ e.g. $name and $Name refer to different variables.

3

Page 4: Php using variables-operators

Assigning Values to Variables

៚ $var = val;

៙ e.g. assigningValues2Variables.php<?php $language = "PHP"; ?>

<h1>Welcome <?php echo $language;

?></h1>

៚ Dynamic variable's name

៙ e.g. dynamicVariableName.php<?php

$clone = "real";// create a

value of${$clone} =echo $real;?>

new variable dynamically$clone"REAL";// output: REAL

at run time from the

Is it possible for a variable's name itself to be a variable?

៚ echo(): print the value of a variable

4

Page 5: Php using variables-operators

Destroying Variables

៚ e.g. destroyingVariables.php<?php

$apple = "Apple";echo $apple; // output: Apple// unset()unset($apple);echo $apple; // error: Undefined

variable

$banana = "Banana";

echo $banana; //

// NULL value$banana = NULL;echo $banana; //?>

output: Banana

output: (nothing)

5

Page 6: Php using variables-operators

Inspecting Variable Contents

៚ e.g. inspectingVariableContents.php<?php

$apple = "Apple"; $yr = 2011;

// var_dump()

var_dump($apple);var_dump($yr); //

// output: string(5)

output: int(2011)"Apple"

// print_r()

print_r($apple); // output: Apple

print_r($yr);

?>// output: 2011

6

Page 7: Php using variables-operators

Understanding PHP’s Data Types

៚ Data type is the values assigned to a variable.

៚ Booleans

៙ 1 (true) or 0 (false)

៚ 2 numeric

៙ Floating-point values (a.k.a floats or doubles) are decimal or fractional numbers,

៙ While integers are round numbers. ៚ Non-numeric: String

៙ Be enclosed in single quotes (') or double quotes (")

៚ NULL (a special data type in PHP4)

៙ Represent empty variables; a variable of type NULL is a variable withoutany data.

A NULL value is not equivalent to an empty string "".

7

Page 8: Php using variables-operators

Understanding PHP’s Data Types (cont.)

៚ e.g. hexadecimal_octal_scientificNotation.php<?php$dececho

= 8; // decimal$dec; // output: 8

$octecho

= 010; // octal$oct; // output: 8

$hexecho

= 0x5dc;$hex; //

// hexadecimaloutput: 1500

// scientific notation$sn1$sn2echo?>

= 6.9e+2;= 6.9e-2;$sn1." ".$sn2; // output: 690 0.069

8

Page 9: Php using variables-operators

Setting and Checking Variable Data Types

៚ e.g. setting_CheckingVariableDataTypes.php<?php

$apple = "Apple";echo gettype($apple); // output: string

$yr = 2011;echo gettype($yr); //

output: integer

$valid = true;echo gettype($valid); // output : boolean

echo gettype($banana); // output: NULLvariable)

(error: Undefined

$empty = NULL;echo gettype($empty); // output: NULL?>

9

Page 10: Php using variables-operators

Setting and Checking Variable Data Types (cont.)

៚ e.g. casting.php<?php

$f_speed =

$i_speed =// output:

36.9; // floating-point

(integer)

36.9$f_speed; // cast to integer

echo $f_speed;

// output: 36echo

?>$i_speed;;

10

Data Type Checking Functions

Function Purpose

is_bool Test if holding a Boolean value

is_numeric Test if holding a numeric value

is_int Test if holding an integer value

is_float Test if holding a float value

is_string Test if holding a string value

is_null Test if holding a NULL value

is_array Test if being an array

is_object Test if being an object

Page 11: Php using variables-operators

Using Constants

៚ define(CONST, val);

៚ Constant names follows the same rules as variable names but not the $

៚ e.g. usingConstants.php<?php

define("APPLE", "Apple");define("YR", 2011);

prefix.

// output: Apple 2011

echo

?>APPLE." ".YR;

Constants name are usually entirely UPPERCASED.

When should we use a variable, and when should we use a constant?

11

Page 12: Php using variables-operators

Manipulating Variables with Operators

៚ Operators are symbols that tell the PHP processor to perform certain actions.

៚ PHP supports more than 50 such operators, ranging from operators for arithmetical operations to operators for logical comparison and bitwisecalculations.

12

Page 13: Php using variables-operators

Performing Arithmetic Operations

៚ e.g. arithmeticOperations.php<?php

echo 3 + 2; // output: 5

echo 3 - 2; // output: 1

echo 3 * 2; // output: 6

echo 3 / 2; // output: 1.5

echo

?>3 % 2; // output: 1

Is there any limit on how large a PHP integer value can be?

13

Arithmetic Operators

Operator Description

+ Add

- Subtract

* Multiply

/ Divide

% Modulus

Page 14: Php using variables-operators

Concatenating Strings

៚ e.g. concatenatingStrings.php

<?php$apple = "Apple";$banana = "Banana";

// use (.) to join strings into 1

$fruits = $apple." and ".$banana;

// output: I love apple and

".$fruits.".";banana..

echo

?>"I love

14

Page 15: Php using variables-operators

Comparing Variables

៚ e.g. comparingVariables.php<?php$num = 6; $num2 = 3; $str = "6";

// output:echo ($num// output:echo ($num

0<1>

(false)

$num2); (true)$num2);

// output:echo ($num

0<

(false)$str);

// output:echo ($num// output:echo ($num?>

1 (true)== $str);0 (false)=== $str);

15

Comparison Operators

Operator Description

== Equal to

!= Not equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

=== Equal to and of the same type

Page 16: Php using variables-operators

Performing Logical Tests

៚ e.g. performingLogicalTests.php<?php

// output:

echo (true// output:echo (true// output:

1 (true)

&& true);0 (false)&& false);0 (false)

echo (false && false);

// output: 1 (true)

echo (false || true);

// output: 0 (false)

echo (!true);?>

16

Logical Operators

Operator Description

&& AND

|| OR

! NOT

Page 17: Php using variables-operators

Other Useful Operators

៚ e.g. otherUsefulOperators.php

<?php$count = 7; $age = 60; $greet = "We";

Increased by 1: ++Decreased by 1: --

$count -= 2;

// output: 5 echo $count;

e.g. $count++; // $count = $count + 1;

$age /= 5;

// output: 12 echo $age;

$greet .= "lcome!";

// output: Welcome!echo $greet;?>

17

Assignment Operators

Operator Description

+= Add, assign

-= Subtract, assign

*= Multiply, assign

/= Divide, assign

%= Modulus, assign

.= Concatenate, assign

Page 18: Php using variables-operators

Understanding Operator Precedence

៚ Operators at the same level have equal precedence:

៙ ++

៙ !

៙ *

៙ +

៙ <

៙ ==

៙ &&

៙ || ៙ =

--

/

-<=!=

%

.> >==== !==

+= -= *= /= .= %= &= |= ^=

៚ Parentheses (

៚ e.g.

៙ 3 + 2 * ៙ (3 + 2)

): force PHP to evaluate it first

5; // 3 + 10 = 13

* 5; // 5 * 5 = 25

18

Page 19: Php using variables-operators

Handling Form Input

៚ e.g. chooseCar.html

<form name="fCar" method="POST" action="getCar.php">

<select name="selType"><option value="Porsche">Porsche</option><option value="Ford">Ford</option>

</select> Color:<input

<input</form>

type="text" name="txtColor" />type="submit" value="get Car" />

action="getCar.php"Reference a PHP script

method="POST" Submission via POST GET: method="GET"

19

Page 20: Php using variables-operators

Handling Form Input (cont.)

៚ e.g. getCar.php

<?php// get values via $_POST | $_GET$type = $_POST["selType"];$color = $_POST["txtColor"];echo $color." ".$type;

?>

$_POST[fieldName];

$_POST: a special container variable (array) is used to get a value of a fieldof a form sent by using the POST method (or $_GET for the GET method).fieldName: the field whose value will be get/assigned to a variable.

20

Page 21: Php using variables-operators

The End

21

The End