Documente Academic
Documente Profesional
Documente Cultură
PHP
Yahoo.com
PHP vs HTML
PHP executed on server
HTML directly rendered on browser
Basic Syntax
<?php
?>
Comments in PHP
EXAMPLE:
<head></head>
<body class="page_bg">
Hello, today is <?php
echo date('l, F jS, Y') . “© <small>Bok</small>”;
echo
?>.
</body>
</html>
Case Sensitivity in PHP
<?php
$var = 50;
if ($var>0){
echo ("Positive as \n");
echo ("greater than 0");
}
?>
References
https://www.geeksforgeeks.org
PHP Data Types
Specifies the amount of memory that allocates to the variable associated with
it.
PHP basic data types
Scalar
Compound
special
Scalar data types
Whole number
Within the set {…-3,-2,-2,0,1,2,3…}
$population = 20000000;
$temperature = -14 ;
Float Scalar data types
A sequence of characters
Can be text, content of an image file etc.
Example:
$today = 'Today is a nice day';
Compound data types
Resource
Used for variables that hold data or reference of an external resource
Null
Does not contain any value
PHP variables
PHP is a loosely typed programming language, means that when you define a
variable, you don’t specify its data type.
Type casting
PHP handles the type conversion automatically based on the context in which
you use the variables.
Example:
<?php
$x = 20;
$y = '10'; // a string
$z = $x + $y; // $y is converted to an integer
echo $z; // 30
Type casting Cont’d.
Example:
<?php
$x = 20;
$y = '10'; // a string
$z = $x + (int)$y; // $y cast to an integer
echo $z; // 30
Finding data types of variables
PHP provides a built-in function gettype() that returns the data type of a variable
Example:
<?php
$int = 10; // integer
$str = 'this is a string'; // string
$bool = true; // boolean
$d = 3.14; // float
}
Checking NULL and empty
Example:
<?php
$x = null;
echo is_null($x) ? '$x is null' : '$x is not null';
$x = 20;
echo is_null($x) ? '$x is null' : '$x is not null';
empty() function
Example:
<?php
$x = 0;
echo empty($x) ? '$x is empty' : '$x is not empty';
$s = ‘’’’;
echo empty($s) ? '$s is empty' : '$x is not empty';
Variable Scopes
// variable 2
$y = 4;
Syntax:
$var = (condition)? value1 : value2;
Example:
<?php
$x = -12;
echo ($x > 0) ? 'The number is positive' : 'The number is negative';
?>
Assignment Operators
Assignment Operators Example
Example: $y = 30;
<?php $y *= 20;
// simple assign operator echo $y, "\n";
$y = 75; // Divide then assign(quotient) operator
echo $y, "\n"; $y = 100;
// add then assign operator $y /= 5;
$y = 100; echo $y, "\n";
$y += 200; // Divide then assign(remainder) operator
echo $y, "\n"; $y = 50;
// subtract then assign operator $y %= 5;
$y = 70; echo $y;
$y -= 10;
echo $y, "\n"; ?>
// multiply then assign operator
Array Operators
Array Operators Example
Example:
<?php
$x = array("k" => "Car", "l" => "Bike");
$y = array("a" => "Train", "b" => "Plane");
var_dump($x + $y);
var_dump($x == $y) + "\n";
var_dump($x != $y) + "\n";
var_dump($x <> $y) + "\n";
var_dump($x === $y) + "\n";
var_dump($x !== $y) + "\n";
?>
Increment/Decrement Operators
Increment/Decrement Operators
Example:
<?php $x = 2;
$x = 2; echo $x--, " First prints then decrements
echo ++$x, " First increments then prints \n";
\n"; echo $x;
echo $x, "\n";
$x = 2; ?>
echo $x++, " First prints then increments
\n";
echo $x, "\n";
$x = 2;
echo --$x, " First decrements then prints
\n";
echo $x, "\n";
String Operators Example
<?php $x .= $y . $z;
echo $x;
$x = "Geeks";
$y = "for"; ?>
$z = "Geeks!!!";
Spaceship Operators (Introduced in PHP 7)
Used to compare values but instead of returning Boolean result, it returns integer
values
If both the operands are equal, it returns 0.
If the right operand is greater, it returns -1
If the left operand is greater, it returns 1
Spaceship Operators (Introduced in
PHP 7)
Example: // We can do the same for
<?php Strings
$x = 50; $x = "Ram";
$y = 50; $y = "Krishna";
$z = 25; echo $x <=> $y;
echo $x <=> $y; echo "\n";
echo "\n"; echo $x <=> $y;
echo $x <=> $z; echo "\n";
echo "\n"; echo $y <=> $x;
echo $z <=> $y; ?>
echo "\n";
Operator Precedence
if statement
if…else statement
if…elseif…else statement
switch statement
if statement Flow Chart
if…else Statement Flow Chart
if…elseif…else Statement
Flow Chart
switch
Statement
Ternary Operators
Syntax: }
(condition) ? if TRUE execute
this : otherwise execute this; // This whole lot can be
Example: written in a
<?php // single line using ternary
$x = -12; operator
if ($x > 0) { echo ($x > 0) ? 'The number is
positive' :
echo "The number is
positive \n"; 'The number is
negative';
}
?>
else {
echo "The number is
negative \n";
PHP | Loops
$scores = [1,2,3];
foreach ($scores as &$score) {
$score *= 2;
}
print_r($scores); // [2, 4, 6]
PHP foreach – used in associative array
Example:
<?php
$name = [
'firstname' => 'John',
'lastname' => 'Doe',
'middlename' => 'Bray'
];
foreach ($name as $key => $value) {
echo $key . ':' . $value . '<br>';
}
PHP foreach – iterate over public
properties of an object
Example: $this->lastName = $lastName;
class Person $this->middleName = $middleName;
{ }
public $firstName; }
public $lastName;
public $middleName; $john = new Person('John', 'Doe', 'Bray');
Syntax: {
function function_name(){ echo "This is Geeks for Geeks";
executable code; }
}
Example: // Calling the function
funcGeek();
<?php
?>
function funcGeek()
References
https://www.tutorialspoint.com/php/php_variable_types.htm
http://www.phpknowhow.com/basics/data-types/
https://www.geeksforgeeks.org/php-variables-data-types/
https://tutorialink.com
https://www.guru99.com
https://www.learn-php.org
https://www.homeandlearn.co.uk