PHP Programming Basics

The following program is a simple PHP program that displays the
message “Hello, World!”:
<html>
<head>
<title>Look out World</title>
</head>
<body>
<?php echo ‘Hello, World!’ ?>
</body>
</html>
In this example, the PHP echo command produces output (the string
“Hello, World!”) which is inserted in the output file. The tags
<?php and ?> encloses the PHP commands, which are
processed by the web server to generate the required HTML page.
Language Basics:
PHP is an Open Source Scripting language, which is strongly
influenced by other languages, such as Perl and C. A statement in
PHP is a collection of PHP code that does something. It can be as
simple as a variable assignment or as complicated as a loop with
multiple exit points.
PHP uses semicolons to separate simple statements. A compound
statement that uses curly braces to mark a block of code, such as
conditional test or loop, doesn’t need a semicolon after the
closing brace.
PHP is a case-insensitive language. It doesn’t consider the case
for built-in constructs and keywords. Thus, the following three
lines of code are equivalent in PHP:
echo(“Hello, World!”);
ECHO(“Hello, World!”);
EcHo(“Hello, World!”);
The names of user-defined classes and functions are also
case-insensitive in PHP. But for the ordinary variables (identifiers
used for naming memory locations), PHP becomes case-sensitive. That
is,
$name $NAME $NaMe
are three different variables in PHP.
Literals and Identifiers:
A literal is a data value that appears directly in a program. The
following are literals in PHP:
2001
0xFE
1.4142
‘Hi’
“Hello, World!”
true
null
An identifier is simply a name. In PHP, identifiers are used to name
variables, functions, constants and classes. Variable names always
begin with a dollar sign ($) and are case-sensitive. Where as,
function names and class names are case-insensitive.
The rules for forming a valid identifier in PHP are as follows:
- The first character of an identifier must be an ASCII letter (uppercase or lowercase), the underscore (_), or any of the characters between ASCII 0x7F and ASCII 0xFF.
- After the initial character, we can have characters from alphabets, numbers or special characters (except white space).
Some valid variable names are given below:
$bill
$head_count
$MaxForce
$_underscore
$_int
Here are some illegal variables names:
$not valid
$3Wa
Constants:
A constant is an identifier given for a simple value; only scalar
values – Boolean, integer, double and string – can be constants.
A constant can be defined using the function define() as
follows:
define(‘PUBLISHER’, “O’ Reilly & Associates”);
echo PUBLISHER;
The above set of statements defines constant named PUBLISHER, which
is used in the second statement for displaying its content “O’
Reilly & Associates”.
Once set, the value of a constant can’t be changed with in the
same program. But they can be referenced later by their identifier
any number of times in a program. The main advantage of using
constants is that their value can be changed, if required by changing
the value only in its definition.
Keywords:
A keyword is a word reserved by the language for its core
functionality. We can’t make use of keywords as identifiers for
variables, functions or classes. Some of the keywords defined in PHP
are:
and
array()
break
class
case
catch
endif
exit()
echo()
if
All keywords in PHP are case-insensitive.