PDA

View Full Version : PHP Tutorial: Introduction to PHP



T0X!C-uk
28-06-2005, 11:43 PM
Ok as request by people i have written a brief introduction to php which if read properly should get you on the road :p

The tutorial has taken me just over 2 days to write so i hope you appriciate it. Would love Rep if you like it ;)

PHP - Syntax
----Syntax - The rules that must be followed to write properly structured code.

PHP's syntax are similar to most other programming languages (C, Java, Perl) with the addition that all PHP code is contained with a tag, of sorts. All PHP code must be contained within the following...



<?php
?>

or the shorthand PHP tag that requires shorthand support to be enabled on your server...

<?
?>
If you are writing PHP scripts and plan on distributing (Selling) them, I suggest that you use the standard form (which includes the ?php) rather than the shorthand form. This will ensure that your scripts will work, even when running on other servers with different settings.

How to Save Your PHP Pages
If you have PHP inside your HTML, and you want the web browser to display it correctly you must save the file as .php instead of .html. So be sure to check that you are saving your files correctly. Instead of index.html, it should be index.php if there is PHP inside included in the file.

Example Simple HTML & PHP Page
Below is an example of one of the easiest PHP and HTML page that you can create and still follow web standards.



<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello Liam!";
?>
</body>
</html>
If you save this file and place it on PHP enabled server then you should see "Hello Liam!" displayed. If not, please check that you followed my example correctly.

I used the PHP function echo to write Hello World and we will be talking in greater depth about this PHP function and others, later on in this or later tutorials (If i get around to writing them :p).

The Semicolon!
As you may or may not have noticed in the above example, there was a semicolon after the line of PHP code. The semicolon is the end of a PHP statement and should never be forgotten. For example, if I repeated "Hello Liam!" several times, then I would need to place a semicolon at the end of each statement.

White Space
As with HTML, whitespace is ignored between lines of code. This means it is OK to have one line of PHP code, then 20 lines of blank space before the next line of PHP code.

Example:


<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";








echo "Hello World!";
?>
</body>
</html>
[OK next is variables if you are not already lost then great continue to the next bit but if you require help with any of the above please PM me and i will help you the best i can.]

PHP - Variables
f you have never had any programming, Algebra, or scripting experience, then the concept of variables will be a new to you.

A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value, over and over again.
In PHP you define a variable with the following form:

$variable_name = Value;

If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!

A Quick Variable Example
Say that we wanted to store the values that I talked about in the above paragraph. How would I go about doing this? We would first want to make a variable name and then set that equal to the value we want. See my example below for the correct way to do this.



<?php
$hello = "Hello World!";
$a_number = 4;
?>
[As you may have noticed this is a long tutorial but is worth it :p. If you start to get bored dont forget this will always be here you can remember where you are then go out for a bit then come back. It is better to have breaks in between as it helps you to take more in :)]

PHP - Echo
As you saw in the previous lesson, the PHP function echo is a means of outputting text to the web browser. Throughout your PHP career you will be using the echo function more than any other.

Outputting a String
To output a string use the PHP echo function. You can place either a string variable or you can use quotes, like I have done below, to create a string that the echo function will output.



<?php
echo "Hello!";
echo "<h5>I love Liam :p</h5>";
?>
In the above example I output Hello! without a hitch. To review, this text we are outputting is being sent to the user as a web page, so it is important that we use proper HTML syntax!

In the second line I used echo to write a valid Header 5 HTML statement! To do this I simply put the <h5> at the beginning of the string and closed it at the end of the string. Just because you're using PHP to make web pages does not mean you can forget about HTML syntax! HTML plays a big part in most PHP coding. For example Habboxforum May look like a snazzy php full vbulletin but if you have ever saw a vbulletin file you would see a great deal of HTML coding mixed with PHP.

Be Careful When Echoing Quotes!
It is pretty cool that you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! The echo function uses quotes to define the beginning and end of the string, so you must use one of the following tactics if your string contains quotations:

* Don't use quotes inside your string
* Escape your quotes that are within the string with a slash. To escape a quote just place a slash directly before the quotation mark, i.e. \"
* Use single quotes (apostrophes) for quotes inside your string.

See my example below for the right and wrong use of the echo function:



<?php
echo "<h5 class="specialH5">I love Liam!</h5>";
// This won't work because of the quotes around specialH5!

echo "<h5 class=\"specialH5\">I love Liam!</h5>";
// OK because we escaped the quotes!

echo "<h5 class='specialH5'>I love Liam!</h5>";
// OK because we used an apostrophe '.
?>
Echoing Variables
Echoing variables is easy. The PHP developers put in some extra work to make the common task of echoing all variables nearly foolproof! No quotations are required, even if the variable does not hold a string. Below is the correct format for echoing a variable.


<?php
$my_string = "Hello HBF Members. My name is: ";
$my_letter = Liam;
echo $my_string;
echo $my_letter;
?>
OutPut:


Hello HBF Members. My name is:Liam

Part 2 to this MASSIVE tutorial will be up sometime tomorrow. Getting late now and been working all day so i need some sleep :p

Thanks for reading hope you learned something from this part :)

Thread closed by ,Jess, (Forum Super Moderator): Due to bump.

BlueTails
28-06-2005, 11:50 PM
GREAT!

Mod Please Stick x]

-JT-
14-07-2005, 11:49 PM
this needs stickied!!! AMAZING!!!

Y!
15-07-2005, 12:04 AM
Good tutorial, should be stickied!

-sFusion-
15-07-2005, 12:07 AM
honestly, i couldnt figure out wth you were talking about. i guess i'm not very computery :p

splintercell!
15-07-2005, 06:45 AM
good tut + rep should be stikied.

Tomm
15-07-2005, 07:04 AM
very very basic no gd for advanced users but v v gd for newbies to PHP. Well done!

splintercell!
15-07-2005, 07:11 AM
Proably why its called an introduction to php.

T0X!C-uk
15-07-2005, 09:52 AM
Proably why its called an introduction to php.

Exactly :p

Pauls
06-01-2006, 01:31 AM
Nice tutorial dude

Arisham
19-04-2006, 07:39 PM
Nice tutorial. Don't use echo all the time though. You can use an alternative, print.


<?php
print "Hello World!";
?>

And dude, you also should have explained IF/Else statements because they are very simple too. And not forgetting, you don't have to use echo("");. You can always have echo''; or echo"";.

Still, excellent tutorial!

Temporary
20-04-2006, 12:59 AM
nice tutorial.

Catchetat
21-04-2006, 04:28 AM
You should include a screenie for every step. I don't really get it lol. Guess I'll stick with HTML :(

Billabong
29-04-2006, 09:58 AM
Shame its ripped

http://www.tizag.com/phpT/syntax.php

T0X!C-uk
03-11-2008, 03:00 PM
Billabong evidently you don't do enough research, If you checked with the website you stated, you would see I wrote the tutorial and helped develop the website. I'm good friends with the webmaster so I expect an apology when you return.

My return after years of not using habbo and i'm already explaining myself on a 2 year old thread LOL. Wonder why I left......

Invent
03-11-2008, 03:08 PM
Billabong evidently you don't do enough research, If you checked with the website you stated, you would see I wrote the tutorial and helped develop the website. I'm good friends with the webmaster so I expect an apology when you return.
Last Activity: 11-06-2006 09:16 AMHmmm...o_o

Tomm
03-11-2008, 04:02 PM
Was it really needed to bump such a old thread? Even if he was right I doubt anyone still cares now.

Edited by Meti (Forum Moderator): Please do not tell users that they have broken the forum rules, instead report the post using the report post button.

Jackboy
03-11-2008, 04:19 PM
Was it really needed to bump such a old thread? Even if he was right I doubt anyone still cares now.

Adding on to that, this thread really isn't that useful because people that want to learn php know most of this anyway.

Glasses
03-11-2008, 04:25 PM
And then theres meti who doesn't even close the thread :/

Edited by Meti (Forum Moderator): Please don't post to cause arguments.

Want to hide these adverts? Register an account for free!