Professional Documents
Culture Documents
<html>
<head> <title>Hello World</title>
</head>
<body>
<? php echo "Hello, World!";?>
</body>
</html>
Note :The PHP echo statement is often used to
output data to the screen.
PHP variables
<? php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
After the execution of the statements above, the
variable $txt will hold the value Hello world!, the
variable $x will hold the value 5, and the variable $y will
hold the value 10.5.
Note: When you assign a text value to a variable, put
quotes around the value.
Output Variables
The PHP echo statement is often used to output
data to the screen.
The following example will show how to output
text and a variable:
Example
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
A variable can have a short name (like x and y) or a more descriptive
name (age, carname , total_volume).
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the
variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two different
variables)
Note :Remember that PHP variable names are case-sensitive!
PHP has a total of eight data types which we use to construct our variables
Integers − are whole numbers, without a decimal point, like 4195.
Doubles − are floating-point numbers, like 3.14159 or 49.1.
Booleans − have only two possible values either true or false.
NULL − is a special type that only has one value: NULL.
Strings − are sequences of characters, like 'PHP supports string operations.'
Arrays − are named and indexed collections of other values.
Objects − are instances of programmer-defined classes, which can package
up both other 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).
The first five are simple types, and the next two (arrays and objects) are
compound - the compound types can package up other arbitrary values of
arbitrary type, whereas the simple types cannot.
PHP Variables Scope
or Or $x or $y True if either $x or
$y is true
|| Or $x || $y True if either $x or
$y is true
<? php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Output "Have a good day!" if the current time is less than
20, and "Have a good night!" otherwise.
PHP - The if...else if...else Statement
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Output "Have a good morning!" if the current time is less than 10, and
"Have a good day!" if the current time is less than 20. Otherwise it will
output "Have a good night!“.
PHP switch Statement
<? 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!";
}
?>
Output: Your favorite color is red!