PHP Intro – Part 2
December 12, 2011
Starting Tags
You can generally use either one depending on the php.ini file but to start you php you can do.
<?php
?>
Or you could do (which is the short-hand code)
<?
?>
Echoing
This is probably the easiest function there is in PHP. You can echo html and other PHP as well.
to echo you first need to start with your PHP tags so lets begin
<?php
echo("hello world");
?>
That code will echo out the words “hello world”. Now you can also echo html:
<?php
echo("<i><b>Hello World</b></i>");
?>
This code will display “Hello World” just like that. Other things to note are that if you are echoing things such as links it is highly recommended to use single quotes, or you will end up with a parse error. To counter the parse error do something like this.
<?php
echo '<a href=\'http://nukewarz.com\'>Nukewarz</a>';
?>
Including
This is also very simple. Basically you put the requested file onto that page such as a php, html, text, or even a flash file. To do this simply enter the following code
<?php
include("/directory/file.html");
?>
What this code will do is put a html on the which ever page you insert this code. Now things to note are, if a file is farther up a directory you must add ../ so it will include a file in the previous directory. Example:
<?php
include("../etc/members.php");
?>
Say you were in the directory called “tutorials” this would request the file members.php even though it is located in a different directory all together.
If’s and Else’s
This is also quite simple. Say you had a decent member system with basic cookies. the following would work for example:
<?php
if ($_COOKIE[username]) {
echo("<h2>Login</h2>"); //displays the title
include("../login/welcome.php"); //includes the shouts
}
?>;
This code is basically stating that if the user is logged in (assuming your using cookies) then it will show the shout box. Everything between the {} tags is what happens if there logged in. Here is another example:
<?php
if (!$_COOKIE[username]) {
echo("You are not logged in!"); //tells the user he isn't logged in
die(); //kills the script. Anything ran below or after this page will not be ran.
}
?>
By adding the exclamation mark this is telling the script basically that he is NOT logged in then tells the user he isn’t.
Categorised as: PHP Introduction
So far this is my best tutorial, hope i can produce more.
Great site, keep it up