Professional Documents
Culture Documents
Variables, Data Types, Statements, Operators, Loops
Variables, Data Types, Statements, Operators, Loops
Variables, Data Types, Statements, Operators, Loops
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
</body>
</html>
Declaring PHP variables
In PHP7, you can create a Array constant using the define() function.
The example below creates an Array constant:
Example:
<?php
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];
?>
Constants are Global
Constants are automatically global and can be used across the entire
script.
The example below uses a constant inside a function, even if it is
defined outside the function:
Example:
<?php
define("GREETING", "Welcome to W3Schools.com!");
function myTest() {
echo GREETING;
}
myTest();
?>
PHP Magic Constants
Magic constants begin with two underscores and end with two
underscores. The following section describes some of the most
useful magical PHP constants.
__LINE__
The __LINE__ constant returns the current line number of the file,
like this:
Example
<?php
echo "Line number " . __LINE__ . "<br>"; // Displays: Line number 2
echo "Line number " . __LINE__ . "<br>"; // Displays: Line number 3
echo "Line number " . __LINE__ . "<br>"; // Displays: Line number 4
?>
__FILE__
The __FILE__ constant returns full path and name of the PHP file
that's being executed. If used inside an include, the name of the
included file is returned.
Example
<?php
// Displays the absolute path of this file
echo "The full path of this file is: " . __FILE__;
?>
__DIR__
The __DIR__ constant returns the directory of the file. If used inside
an include, the directory of the included file is returned. Here's an
example:
Example
<?php
// Displays the directory of this file
echo "The directory of this file is: " . __DIR__;
?>
__FUNCTION__
The __FUNCTION__ constant returns the name of the current function.
Example
<?php
function myFunction(){
echo "The function name is - " . __FUNCTION__;
}
myFunction(); // Displays: The function name is - myFunction
?>
__CLASS__
The __CLASS__ constant returns the name of the current class. Here's an example:
Example
<?php
class MyClass
{
public function getClassName(){
return __CLASS__;
}
}
$obj = new MyClass();
echo $obj->getClassName(); // Displays: MyClass
?>
__METHOD__
The __METHOD__ constant returns the name of the current class method.
Example
<?php
class Sample
{
public function myMethod(){
echo __METHOD__;
}
}
$obj = new Sample();
$obj->myMethod(); // Displays: Sample::myMethod
?>
__NAMESPACE__
The __NAMESPACE__ constant returns the name of the current namespace.
Example
<?php
namespace MyNamespace;
class MyClass
{
public function getNamespace(){
return __NAMESPACE__;
}
}
$obj = new MyClass();
echo $obj->getNamespace(); // Displays: MyNamespace
?>
Data Types in PHP
PHP has a total of 8 Data Types which we use to declare variables:
Integer: are the whole number, without a decimal point, eg. $var = 12345;
Double: are floating-point number can contain decimal number, eg. $num =
2.2888800;
Boolean: can have only two possible values, true or false, eg. $x = true. Boolean
value are often used in condition testing.
NULL: is a special type that only has one value:NULL, eg. $my_var = NULL.
String: is a collection of characters, eg. $str = "This is a string". A string can be
any text inside quotes. You can use single or double quotes to represent a string.
Arrays: An array stores multiple values in one single variable, eg. $colors =
array("Red","Green","Blue");
Objects: are instances of a class, which can package up both kinds of values and
functions that are specific to the class.
Resources: are special variables that hold references to resources external to
PHP (such as database connections).
Difference between echo and
print in PHP
PHP echo and print both are PHP Statement.
Both are used to display the output in PHP.
echo
echo is a statement i.e used to display the output. it can be used with
parentheses echo or without parentheses echo.
echo can pass multiple string separated as ( , )
Echo "This ", "string ", "was ", "made ", "with multiple parameters ",
"separated with comma (\,)" ;
echo doesn’t return any value
$r = echo $name; //Error
echo is faster then print
Print
Print is also a statement i.e used to display the output. it can be used
with parentheses print( ) or without parentheses print.
using print can doesn’t pass multiple argument.
print "Hello ", "World"; //Error
print always return 1. So it can be used in expressions.
it is slower than echo
Examples
Echo Print
<?php <?php
// Displaying HTML code // Displaying HTML code
echo "<h4>This is a simple print "<h4>This is a simple
heading.</h4>"; heading.</h4>";
echo "<h4 style='color: red;'>This print "<h4 style='color: red;'>This
is heading with style.</h4>"; is heading with style.</h4>";
?> ?>
Embedding HTML inside Embedding PHP Inside
PHP HTML
<?php <?php
$date = 26; $text= "Enter username";
$month = "January"; ?>
echo "Republic Day is on ".$date." <input type="text" value="<?php
".$month.""; echo $text; ?>">
?>
PHP Operators
+ Addition $a + $b
- Subtraction $a - $b
* Multiplication $a * $b
/ Division $a/ $b
% Modulus $a% $b
Arithmetic Addition Operator
$a= 10;
$b = 6;
echo $c = $a+ $b;
Arithmetic Subtraction Operator
$a= 10;
$b = 6;
echo $a- $b;
Arithmetic Multiple Operator
$a= 10;
$b = 6;
echo $a* $b;
Arithmetic Division Operator
$a= 10;
$b = 6;
echo $a/ $b;
Arithmetic Modulus Operator
$a= 10;
$b = 6;
echo $a% $b;
PHP Assignment Operators
$a= 20;
$a+= 100;
echo $a;
$a= 50;
$a-= 30;
echo $a;
$a= 5;
$a*= 6;
echo $a;
$a= 10;
$a/= 5;
echo $a;
$a= 15;
$a%= 4;
echo $a;
PHP Comparison
Operators Operato Name Exampl Result
r e
== Equal $a== $b Returns true if $a is equal to $b
=== Identical $a=== Returns true if $a is equal to $b, and
$b they are of the same type
The PHP comparison
operators are used to != Not equal $a!=$b Returns true if $a is not equal to $b
compare two values !== Not identical $a!==$b Returns true if $a is not equal to $b, or
(number or string) they are not of the same type
> Greater than $a> $b Returns true if $a is greater than $b
< Less than $a < $b Returns true if $a is less than $b
>= Greater than $a >= Returns true if $a is greater than or
or equal to $b equal to $b
<= Less than or $a <= Returns true if $a is less than or equal
equal to $b to $b
Equal
$a = 100;
$b = "100";
var_dump($a == $b); // returns true because values are equal
Identical (Exact Equal)
$a = 100;
$b = "100";
var_dump($a === $b); // returns false because types are not equal
Not equal
$a = 100;
$b = "100";
var_dump($a != $b); // returns false because values are equal
Not identical
$a = 100;
$b = "100";
var_dump($a !== $b); // returns true because types are not equal
Less than or equal to
$a = 50;
$b = 50;
var_dump($a <= $b); // returns true because $a is less than or equal to $b
Greater than
$a = 100;
$b = 50;
var_dump($a > $b); // returns true because $a is greater than $b
Less than
$a = 10;
$b = 50;
var_dump($a < $b); // returns true because $a is less than $b
Greater than or equal to
$a = 50;
$b = 50;
var_dump($a >= $b); // returns true because $a is greater than or equal
to $b
PHP Increment / Decrement
Operators
The PHP increment operators are used to increment a variable's
value.
The PHP decrement operators are used to decrement a variable's
value.
Operator Name Description
Or
$a = 100;
$b = 50;
PHP has two operators that are specially designed for strings.
PHP also allows you to write code that perform different actions
based on the results of a logical or comparative test conditions at
run time. This means, you can create test conditions in the form
of expressions that evaluates to either true or false and based on
these results you can perform certain actions.
There are several statements in PHP that you can use to make
decisions:
The if statement
The if...else statement
The if...elseif....else statement
The switch...case statement
The if Statement
<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
}
?>
The if...else Statement
<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} else{
echo "Have a nice day!";
}
?>
The if...elseif...else Statement
<?php
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} elseif($d == "Sun"){
echo "Have a nice Sunday!";
} else{
echo "Have a nice day!";
}
?>
switch statement
echo "Today
echo "Today is
case "Tue": break; case "Wed": break; case "Thu":
is Tuesday"; Wednesday
";
echo
echo "Today "Wonder
break; case "Sun": break; default: }
is Sunday"; which day
is this ?";
?>
difference between Print_r and
Var_dump?
var_dump displays structured information about the object /
variable. print_r displays human readable information about the
values with a format presenting keys and elements for arrays
and objects. The most important thing to notice is var_dump will
output type as well as values while print_r does not.
PHP Loops
Loops are use to repeat a block of code again and again upto
specified number of times. In PHP, there are 4 types of loops
available. They are:
for loop.
while loop.
do...while loop.
foreach loop.
PHP For Loop
PHP for loop is divided into 3 parts i.e. initialization, condition and
increment/decrement. Initialization is the initial value from where
the loop start execution, condition will execute a loop till the
specified condition is true and quits the loop if it is false,
increment/decrement will change the variable value for the next
condition.
Example
With a do-while loop, on the other hand, the loop will always be
executed once, even if the conditional expression is false,
because the condition is evaluated at the end of the loop
iteration rather than the beginning.
PHP foreach Loop
foreach($colors as $value){
// Loop through superhero array
echo $value . "<br>";
foreach($superhero as $key => $value){
} echo $key . " : " . $value . "<br>";
?> }
?>