Pre-Info
Before you start coding php, you are going to need the following:
- PHP enabled server
- A Text Editor or DreamWeaver.
Lets Begin
PHP is a language that is embedded into HTML code. PHP is translated into HTML before it reaches the user in their browser.
Example:
PHP
PHP Code:
<html>
<body>
<?PHP echo "Hello World"; ?>
</body>
</html>
HTML
PHP Code:
<html>
<body>
Hello World
</body>
</html>
PHP Syntax
When ever you want to use php code, the code must be inside "<?PHP" & "?>" tags.
Also, every line in PHP must has the semi-colon (
at the end of the line.
Example
PHP Code:
<?PHP
echo "Hello World!<br>";
?>
//Displaying text w/ shorthand tags:
<?="Hello World"?>
Browser Output:
Hello World!
Hello World!
Comments
Like in HTML and many other programming languages, you can have a comment within your code. there are many forms of a comment.
Example
PHP Code:
//One line comment example
/*
Block Commenting
some more of the comment
some more
some more
end of comment
*/
Variables - Strings
A Variable can have either Strings (text) or integers (numbers), and are shown with either a name, or a letter.
A variable always begins with "$" sign and then the name of the string, can be name you want
PHP Code:
//Create A String
$string = "Hello World";
//Display a String
echo $string;
Browser Output:
Hello World
Concatenate
To concatenate 2 strings together you use the . (dot) operator
PHP Code:
$name = "Dylan";
$message = "Hello: ";
echo $message . $Name;
Browser Output:
Hello Dylan
You can also have text and a string concatenated together. We are going to use the date() function to show the time, i will get into this more in a later tutorial.
PHP Code:
$name = "Dylan";
$time = date(g:i);
echo "Hello " . $name . ", It is " . $time;
Browser Output:
Hello Dylan, It is 11:23
String Naming Rules
- Dont use spaces in the name, use underscore (_) or hyphen (-)
- It is not recommended that you named your string in all UPPERCASE
- Choose names that are related to what it contains.
(More on Strings coming soon.)
Variables - Integers
Here i am going to show you how to work with Integers within Variables.
PHP Code:
//Create and Integer
$integer = "1";
echo $integer;
Browser Output:
1
Adding Integers
Adding to an integer is easy, you are going to want to have an integer with a value, lets say 1, and then we are going to add 1 to it.
PHP Code:
//There are 2 ways of doing this, the recommended way:
$int = "1";
$int = $int++;
echo $int;
//Another Way, This way is also acceptable:
$int = "1";
$int = $int + "4";
echo $int;
More on integers coming soon...
EDIT: Thanks Kows and Penagate for corrections!