Basics of PHP
- ID: PHP - 1
- Date Added: 2005-11-10
- Category: PHP
- Difficulty: Easy
- Author: Rei
What is PHP?
According to Wikipedia, PHP isStarting Off
Before you start working with PHP you want to make sure your host supports PHP. Most free web hosting services don't generally offer it, however there are some that do. If you're not sure, we're going to conduct our little test. Copy and paste the following code on your page (In courtesy of DNW).<?php print
// Your message will go between the
quotation marks.
("Hi there this is just a test to see if my PHP works");?>
Now view your page, and if you see no Hi there this is just a test to see if my PHP works, sorry mate, you're out of luck! However if you see
Hi there this is just a test to see if myThat means your host support PHP. What's so great about PHP? Do remember how annoying it was to edit every single page because you typed the wrong URL for your links, or may be a type? Now you only need to edit ONE or TWO at the most in order for it to effect all the files on your server! How great is that? PHP is quite hard at first, but if you take the time to learn it, it comes very handy ^-^ Remember how you had the triangular brakets (< and >) in HTML to execute a command? In PHP it's quite different. This is what I mean you use the following code to command
PHP works :)
<?php // This is where a command is located in order to execute the command.The commands are different. Some of the most common ones are print, echo, include. We will learn about PHP includes later on.
("// This is where your
message usually goes."); ?>
PHP print will print whatever you put between the brackets on your page, just like when we did our experiment a while ago.
PHP echo is exactly like PHP Print, it's more commonly used than PRINT. With PHP echo, you must put a slash before you include any quotation marks (" ").
For Example
<?php echo "<br>";I didn't have to put any around/beside the >br> command because it does not contain any quotation marks. As I studied PHP, there were a few common errors that occured to me very often.
echo "<a href=\"javascript:window.close();\"> Close Window</a>";
echo '<a href="javascript:window.close();"> Close Window</a>;There are four errors within this code. Can you spot them all?
- #1 - We're missing the PHP, generally people find adding PHP after every single <? quite irritating. However I learnt that this error is just like forgetting to add a </style> in stylesheets.
- #2 - Inconsistant quotation. See how after the echo, I placed an apostrophe instead? That is a very very common error that we must avoid at all times.
- #3 - We're missing \ before the quotation mark. You will be getting errors whenever you try to view your page. This makes the PHP think that you've placed all your code and have closed the tag.
- #4 - No quotation mark at the end. After my semicolon, we have to add a quotation mark to tell the browser that I've really placed all my code, and that I'm closing the tag.
Variables
The second most important thing in PHP is variables. The variables are assigned with the $ - dollar sign.<?phpSo instead of typing Welcome to Akatsuki Designs! A site full of goodies and tutorials for visitors! all the time, I can just insert $ad and it will automatically type the whole thing for me; it does makes life a whole lot easier ^.^ Of course you can use it for other uses, for example dates.
// The $ad is our variable.
$e-d = "Welcome to Akatsuki Designs!
A site full of goodies and tutorials for
visitors! ";
// If you wish to have the message
appear without having to type it every single
time, place $e-d instead.
echo "Hi! $ad";
?>
<?php
// Our date
function that is shown here follows that standard PHP Date function.
$date = date("d-m-y");
echo "Today is ".$date; ?>
All I have to insert is <? php $date;, and the date will show up itself. The echo part will print whatever I want it to print. Simple eh? Now see where the line
$date = date("d-m-y");? This agrees with the PHP date function. To see more, please visit the PHP.net Date Function.
IF & Else Statement
The IF and ELSE statement are very easy to understand, and very useful to ^^I'm not sure how to explain it, however it's allows the PHP to execute function A if case A happens. However, if someone chooses Case B, then PHP will execute Function B
<?php
// Variable number one: Chooses whether s/he is a webmaster/mistress.
$web = "Webmistess/master";
// IF s/he selects it, therefore ECHO#1 will appear.
if ($web = "Webmistess/master")
{
// This message appears when a
visitor selects the choice given.
echo "You're a webmistress/master too! Like me ^-^
Wanna be affliates?";
}
// However if s/he does not choose one of the given choice, ECHO#2
will appear instead.
else {
// Your message goes here.
echo "Oh well... may be another time :(";
}
?>
This works best in forms, say I ask a question asking whether you are/are not a webmistress/master. If you click on YES, a message will show up, the message that will show up is You're a webmistress/master too! Like me :) Wanna be affliates?. However, if you click on NO, the other message will show up instead Oh well... may be another time :( Simple?
« back · clear ¦ reload · forward »
Akatsuki Designs © Rei [2006-2008]. Site, layout, and content © Rei. All rights reserved. Valid CSS and HTML
Fight Spam! | Spot a typing error? | HostTracker
Fight Spam! | Spot a typing error? | HostTracker



