What is PHP?PHP is a server-side scripting language. This means that all the PHP code is processed server-side, unlike HTML, so PHP code is usually not for the public to see. It is used for creating dynamic pages.
Using PHPTo begin with, PHP begins and ends with php tags. Everything within these tags will be processed:
<?php
//Code here
?>
Some alternative tags:
<?
//Code here
?>
These tags must be specially configured first:
<%
//Code here
%>
<SCRIPT LANGUAGE="php">
//Code here
</SCRIPT>
Hello World!As a first example of using PHP, we will 'echo' the text "Hello World!":
<?php
echo "Hello World!";
?>
As you can see, the code begins with the PHP tags, and includes this line:
echo "Hello World!";
It's ended by a semi-colon, which is used to end all statements.
The
echo function basically just prints the text within quotes onto the browser screen. Double quotes and single quotes can be used.
VariablesIn PHP, variables are used to store data that can be changed. PHP's variables, unlike C/C++ or Java, can be of any type, so can also be used without specifying a type:
<?php
$variable = "I'm a string!";
echo $variable;
$variable = 1;
echo $variable;
?>
In this example, the variable has changed its content and its type, from a string into an integer. Both variables' contents are printed on the screen. In this example, the
echo function does not use double or single quotes becuase it
only prints out a variable. Anything else, including a combination of variable and text will have to be included within quotes.
RememberPHP is supposed to be used with HTML. You can
echo the HTML within PHP, sure, but it is much better to seperate the HTML and PHP:
<html>
<head>
<title>Hello!</title>
</head>
<body>
Hello, <?php
$name = "Andy";
echo $name;
?>
</body>
</html>
This would print "Hello, Andy". The HTML could have been included in the PHP but it becomes a pain to understand the source when the web page becomes more complicated and large.