You are on page 1of 20

Internet Technology II

Lecture (3)
By : T.Ammar Hashim Mohammed
PHP Data Types

• Variables can store data of different types, and different


data types can do different things. PHP supports the
following data types:
• PHP String
<?php
$x = "Hello world!"; or $y = 'Hello world!';
?>
• PHP Integer
<?php
$x = 5985; $y = 10; ?>
PHP Data Types…con

• PHP Float (double )


<?php
$x = 10.365; ?>
• PHP Boolean
$x = true; $y = false;
Booleans are often used in conditional testing.
• PHP Array
<?php
$cars = array("Volvo","BMW","Toyota"); ?>
PHP Data Types…con
• PHP Object 
• An object is a data type which stores data and information on
how to process that data. A class is a structure that can contain
properties and methods .
<?php
class fname {
var $first;
var $last;
function fname() {
$this->first = “Ammar"; $this->last = “Hashim";
}}
PHP Data Types…con
• create an object
$name = new fname();
• show object properties
echo $name->first; echo $name->last;
?>
• PHP NULL Value 
<?php $x = null; ?>
A variable of data type NULL is a variable that has no
value assigned to it.
PHP Data Types…con
• PHP var_dump() function 
• The PHP var_dump() function returns the data type and
value:
<?php
$x = 10.365;
var_dump($x); // Out Put float(10.365) ?>
• PHP String Functions
• The PHP strlen() function returns the length of a string.
<?php
echo strlen("Hello world!"); // outputs 12 ?>
PHP Data Types…con
• The PHP str_word_count() function counts the
number of words in a string: 
<?php
echo str_word_count("Hello world!"); // outputs 2 ?
>
• The PHP strrev() function reverses a string
<?php
echo strrev("Hello world!"); // outputs !dlrow olleH ?
>
PHP Data Types…con
• The PHP strpos() function (string position)
searches for a specific text within a string.
<?php
echo strpos("Hello world!", "world"); // outputs 6 ?>
• The PHP str_replace() function replaces some
characters with some other characters in a string.
<?php
echo str_replace("world", “Ammar", "Hello
world!"); // outputs Hello Ammar! ?>
PHP Constants

• Constants are like variables except that once


they are defined they cannot be changed or
undefined. 
<?php 
define("GREETING", "Welcome to PHP!"); echo
GREETING;
?>
PHP Conditional Statements
• PHP - The if Statement
• The if statement is used to execute some code only
if a specified condition is true.
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
PHP - The if...else Statement

• Use the if....else statement to execute some code if a


condition is true and another code if the condition is false.
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
PHP - The if...elseif....else Statement

• Use the if....elseif...else statement to specify a new


condition to test, if the first condition is false.
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!"; }
elseif ($t < "20") {
echo "Have a good day!"; } else {echo "Have a good
night!";} ?>
The PHP switch Statement

• Use the switch statement to select one of many blocks of code to be executed.
<?php
$favcolor = "red";
switch ($favcolor) {
case "red": echo "Your favorite color is red!";
break;
case "blue": echo "Your favorite color is blue!";
break;
case "green": echo "Your favorite color is green!";
break;
default: echo "Your favorite color is neither red, blue, nor green!";
} ?>
PHP Operators
PHP Arithmetic Operators
PHP Assignment Operators
PHP Increment / Decrement Operators
PHP Comparison Operators
PHP Logical Operators
PHP String Operators
PHP Array Operators

You might also like