UNIT 1 ----PHP LANGUAGE STRUCUTURE
THE STRUCTURE OF A PHP
<?php
echo
"<p>Hello World!</p>";
?>
•The PHP tags enclose only one statement
an echo statement.
•The echo statement is a PHP statement that
you’ll use frequently.
•The output is simply the text that’s included between
the double quotes.
BUILDING BLOCK OF PHP
PHP supports a
number of fundamental basic
data types, such as integers, floats, and strings.
Basic data types are the simplest building
blocks of a program.
Note: PHP statements
end with a semicolon (;).
BASIC PHP SYNTAX
A PHP script can be placed anywhere in the
document. A PHP script starts with
<?php
and
ends with ?>:
?php
// PHP
code goes here
?>
The default file
extension for PHP files is ".php".
COMMENTS IN PHP
A comment in PHP code
is a line that is not read/executed as part of the program.
Its only purpose is
to be read by someone who is looking at the code.
Example:
// PHP code goes here
Comments
can be used to:
Let others understand
what you are doing.
Remind yourself of
what you did.
PHP CASE SENSITIVITY
In PHP, NO keywords,
classes, functions, and user-defined functions are case-sensitive.
All three echo
statements below are legal
Example
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
PHP VARIABLES
Variables are
"containers" for storing information.
A variable can have a short
name or a more descriptive name.
Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
RULES FOR PHP VARIABLES:
1.A
variable starts with the $ sign, followed by the name of the
variable
2. A
variable name must start with a letter or the underscore
character
3. A
variable name cannot start with a number
4. A
variable name can only contain alpha-numeric characters and
underscores
(A-z, 0-9, and _ )
5.
Variable names are case-sensitive ($age and $AGE are two
different
variables).
Output Variables:
The
PHP echo statement is often used to output data to the screen.
Ex:
<?php
$txt
= "W3Schools.com";
echo "I love $txt!";
echo "I love $txt!";
?>
PHP VARIABLES SCOPE
PHP VARIABLES SCOPE
The scope of a variable is the part of the script where
the variable can be referenced/used.
PHP has three different variable scopes:
1. local
2. global
3. static
PHP ECHO AND PRINT STATEMENTS
Echo and print are more or less the same. They
are both used to output data to the screen.
Echo
echo has no return value.
echo can take multiple parameters.
Print
print has a return value of 1.
print can take one argument.
Note:
echo is marginally faster than print.
PHP DATA TYPE
Variables can store data of different types, and
different data types can do different things.
1.String
A string
is a sequence of characters
Ex:
$x = "Hello
world!";
$y = 'Hello world!';
2.
Integer
An
integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647.
Ex:
<?php
$x = 5985;
var_dump($x);
?>
$x = 5985;
var_dump($x);
?>
3.
Float
Is a number with a decimal point or a number in
exponential form.
Ex:
$x = 10.365;
4. Boolean
A Boolean represents two possible states:
TRUE or
FALSE.
Ex:
$x = true;
$y = false;
$y = false;
5.
Array
An
array stores multiple values in one single
variable.
Ex:
$cars = array("Volvo","BMW","Toyota");
6.
Object
An object is a data
type which stores data and information on how to process that data.
Ex:
$herbie
= new Car();
7.
NULL Value
Null is a special data type whichcan have only one value:
NULL.
Ex:
$x
= null;
8. Resource
The special resource type is not an actual data type.
It is the storing of a reference functions and
resources external to PHP.
PHP CONSTANTS
Constants are like variables except that once they are
defined they cannot be changed or undefined.
A valid constant name starts with a letter or
underscore(no $sign before the constant name).
To create a constant, the define() function.
Syntax:
define(name, value, case-insensitive)
Ex:
define("GREETING", "Welcome to
PHP!");
echo GREETING;
echo GREETING;
PHP
OPERATORS
Operators are used to perform operations
on
variables
and values.
Arithmetic operators -> to perform arithmetical
operations
Assignment operators -> to write a value to a
variable.
Comparison operators ->to compare two values
Increment/Decrement operators -> to increment or
decrement a variable's value.
Logical operators -> to combine conditional
statements.
String operators -> specially designed for strings.
Array operators -> to compare arrays.
Comments
Post a Comment