Nov 8, 2009
Pages: 1, 2

Introduction To PHP - PHP for Beginners!

free web hosting

Read Latest Entries..: (Post #15) by Custergrant on Jul 10 2007, 05:34 PM.
Hey, great tutorial! I'm attempting to convert a forum game into a PHP game, and thanks to this guide, I'm not quite as scared about it as I was!
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Open Discussion & Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > PHP

Introduction To PHP - PHP for Beginners!

rkage
So it's fine telling people how to write scripts but if they don't know what it means, it's pretty useless. Even if you give someone directions to your house, if you haven't taught them how to read directions, the directions are meaningless. With that in mind, I wanted to make a series of tutorials explaining how to write PHP, hoping it will encourage a few user's on the board to jump into the depths of PHP.

How to define PHP code
The Basics

Creating a PHP page is just as simple as saving the page as filename.php as opposed to filename.html. So create a PHP file for practicing the areas covered in this tutorial so you can try the examples for yourself. As long as your host has PHP support then your page should function properly.

The PHP code must begin with a "<?php" tag and end with "?>" so here is a short example;

CODE
<?php
         //Code goes here
?>


Commenting
We comment in PHP by using the double slashes "//" Anything after those slashes on that line are commented. They don't do anything to the code, but if you go back to the code after a long time, or give the code to someone else, it will show what you were trying to achieve with that bit of code. It may seem boring to write a comment after everyline but it will help you in the long run. Not only is it a future reference, but by thinking through the problem in english words, you are more likely to understand how to solve it.
also comment in PHP using a "/*" to mark the start of the commented text and "*/" to mark the end. I only tend to use this if i want to stop a block of code from operating temporarily. Rather than deleting it, I comment it and then uncomment it later. Using the "/* */" syntax to comment allows you to comment multiple lines as opposed to just one line using "//".

CODE
<?php
//This line is commented
This line isn't.
/* These
Lines
Are
Commented */
?>


Outputting Text
The most simple way of outputting text is using the echo function.

CODE
<?php
echo "Hello World";
?>


If we save this code into a file and open that file in a Web Browser we would get the words "Hello World" in black writing. Notice we put double quotes around the text we want to echo. You can use single quotes as well, it doesn't make a difference (although there is some arguments that single quotes are slightly faster when processing but it is insignificant, so nothing to worry about.)
If you are trying to output the text "It was Maggie's" then we need to use double quotes. If we use single quotes, then it would recognise the apostrophe after Maggie as the closing single quote and then the PHP would wonder why there was an "s" and another single quote after that. In otherwords, it will throw up some error messages.

Similarly, if you were trying to output;
Maggie said "That is mine."
Then we will use single quotes.


There is another way around this and that is to use a backslash as an escape character. Look at this bit of code.
CODE
<?php
echo "Maggie said \"That is mine.\"";
?>


Notice how we put backslashes before the double quotes? This tells PHP not to take the next pair of double quotes as closing tags.

The Semi-Colon of doom
You may have also noticed the semi-colon ";" at the ends of the echo function. This is a PHP syntax perk you are going to have to get used to. I am still forgetting to put the semi-colons at the end of my lines and almost half of my errors are specifically that. Basically you need to tell PHP "Hey, this is the end of this line, nothing comes after this so move onto the next line and continue processing". If i done this;

CODE
<?php
echo "I am trying to cause an error"
echo "Will it throw up an error?";
?>


The answer is yes, it will throw up an error. On line 2 to be exact, we have left out our semi-colon. That is one of the great advantages of PHP it will tell you the line number and the nature of the error that is causing your script to not function properly. This makes error fixing ten times easier. You'll still get quite angry with some of the errors though mad.gif but you'll get used to error checking, with the more PHP you encounter.

A new line
The final trick of outputting data in this tutorial is taking a new line. Look at this example;

CODE
<?php
echo "line 1";
echo "line 2";
?>


You may expect this to show up in your browser like this,
CODE
Line 1
Line 2


Why? You haven't told PHP to output line 1, take a new line, and output line 2. You have just told it to output line 1 and output line 2. So you will in fact get;

CODE
Line 1Line 2


So we need to add the return command "\n" where we want the new line to take place. This uses the magical backspace command again, once again it tells PHP to treat the next character specially and PHP will read the n and take a new line.

CODE
<?php
echo "Line 1\n";
echo "Line 2";
?>


That will output the desired result. We print out line 1 and take a new line, then print out line 2. You may notice that you can combine the two lines of code into one statement and still get the same result;

CODE
<?php
echo "Line 1\nLine 2";
?>


Whilst this is perfectly acceptable, it all comes down to user preference and readibility to combine the statements. Say you wanted to edit line 2 and you looked through your code. You'd expect to find it in the second echo statement but in reality, it's in the first. This seems relatively obvious when are lines of text are "line 1" and "line 2" but if they didn't have numbers in it, the "\n" would be hard to spot in amongst loads of text.

So I hope you have enjoyed this tutorial and it has taught you something. Maybe I even helped you create your very first PHP page. Please leave any comments, or suggestions to my tutorial and I will try and implement them into it, if certain areas are vague.

 

 

 


Comment/Reply (w/o sign-up)

harriko
this is really good for me as iam learning php at the momeent in my spare time. you cannot get any simplar than this in a php tutorial, its so simple it could even qualify for to be in the "dummies book of..."

so if anyone is learning php straight from the begining its good to look here. so keep it up the tutorials krage! im probably looking foward for your future tutorial posts on here.

Comment/Reply (w/o sign-up)

Coach
I am starting to learn php using the 5.04 version. I have done the exercises that you gave us and I have noticed that the command "return" does not work, but the <br> does. Has anything been done about this in the last version?

Thank rkage for the valuable help you are bringing to the beginners.

Comment/Reply (w/o sign-up)

h3lium
wow, thanks. i'm just beginning to learn php myself for some projects. this will surely come in handy

Comment/Reply (w/o sign-up)

derouge
A nice introduction .. very useful. When learning a new programming language the simpler explanations, the better.

Comment/Reply (w/o sign-up)

Lomeus
Finaly i have found a tutorial that i understand biggrin.gif
thanx for getting me started
im reading example files for a very long time to try and understand them but that didnt work

Comment/Reply (w/o sign-up)

Houdini
QUOTE(Coach @ Apr 11 2005, 09:16 PM)
I am starting to learn php using the 5.04 version. I have done the exercises that you gave us and I have noticed that the command "return" does not work, but the <br> does. Has anything been done about this in the last version?

Thank rkage for the valuable help you are bringing to the beginners.
*


try
CODE
\n\r

Windows and Unix respond differently, it is not your version but perhaps your platform.
Just another thingy that you might want to try with your code try and let me know if not correct!

Comment/Reply (w/o sign-up)

YudzzY
i do not really know php, but just from the codings i see, i figured out the above!
i have to look forward to more coding details.. however these are very helpful!
well, these were just for displaying text, so are you coming more with the commands and more complex codings ?
i will be waiting for them...


Comment/Reply (w/o sign-up)

kook
sorry plz delete upper post caz i post error(my comp's has problem with javascript)

You can use
CODE
<?
............
?>

or
CODE
<?php
...........
?>

or
CODE
<script language="php">
............
</script>

use echo "............."; when you need type html
use //........ or /*........ or #............. to comments
can use insert in html e.g.
CODE
<html>
<head><title>....</title></head>
<body>
<?
echo "..........";
?>
</body>
</html>

or inset html in php e.g.
CODE
<?php
<html>
<body>
..............
</body>
</html>
?>


$.......... is variable form:
CODE
$variable's name="value";

CODE
<?php
$a="123";
echo "$a";
it'll show 123
symbol
CODE

+ -- add e.g. 10+2 = 12
- -- minus e.g. 10-2 = 8
* -- mutiply e.g. 10*2 = 20
/ -- device e.g. 10/2 = 8
< -- less than e.g. $1<10 =variable $1 has value less than 10
> -- more than e.g. $1>10 =variable $1 has value more than 10
>= -- more than or equal e.g. $1>=10 =variable $1 has value more than 10 or
=10
<= -- less than or equal e.g. $1>=10 =variable $1 has value less than 10 or =10
!= -- don't equal e.g. $1!=10 =variable $1 has value don't equal 10
== -- equal e.g. $1==10 =variable $1 's value is 10
or -- or
! -- not
and -- and


increase value
CODE
<?php
while ($1=10;$1<15;$1++)
{
echo "$1 &nbsp;";
}
?>
it'll show 10 11 12 13 14

if
Form:
CODE
if (function or variable){
.......echo "....";
}else{
.......echo ".....";
}

e.g.
CODE
if (mysql_db_query($dbname,$sql)){
echo "yes";
}else{
echo "no";
}
result:if it do function(mysql_db_query($dbname,$sql))complete it'll show "yes",if it do function(mysql_db_query($dbname,$sql))don't complete it'll show "no"

next row: \n e.g.
CODE
<?
echo "hh"; \n
?>


mysql
you can use phpMyAdmin to handle mysql easier
to connect sql by use code
form:
CODE
<?php
$host="hostname";
$user="username";
$pass="password";
mysql_connect($host,$user,$pass) or die("Can't connect to mysql");
?>

to create db(database)
CODE
<?php
$host="hostname";
$user="username";
$pass="password";
mysql_connect($host,$user,$pass) or die("Can't connect to mysql");
$dbname="dbname";
mysql_create_db($dbname);
?>


this's a past of php i'll post about other later.

 

 

 


Comment/Reply (w/o sign-up)

sirdope
this was pretty helpful as i myself im learning php at the moment, hope to see more in the future. thanks

Comment/Reply (w/o sign-up)

Latest Entries

Custergrant
Hey, great tutorial! I'm attempting to convert a forum game into a PHP game, and thanks to this guide, I'm not quite as scared about it as I was! smile.gif

Comment/Reply (w/o sign-up)

zemon1
im learning php mysql and javascript and that was quite helpful for the php portion, kinda seems a little easier than java...

Comment/Reply (w/o sign-up)

optiplex
This is really good for people like me , thanks

Comment/Reply (w/o sign-up)

juancarlosvergar
Great curse for PHP. I try to apply for my web. I will tell you my avances.

Comment/Reply (w/o sign-up)

Houdini
QUOTE(Coach @ Apr 11 2005, 09:16 PM) *

I am starting to learn php using the 5.04 version. I have done the exercises that you gave us and I have noticed that the command "return" does not work, but the <br> does. Has anything been done about this in the last version?

Thank rkage for the valuable help you are bringing to the beginners.
Actually the \r\n are (return and newline) and can only be seen in the pages source not in your browsr for example write this simple code and then run it in your browser.
CODE
<?php
echo "This is a test line of characters that is very very long\n but with the addition of the newline character it will\n be viewed by viewing the page source and you will see that this long single line show up as 3 lines.";
?>


Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2

See Also,

*SIMILAR VIDEOS*
Searching Video's for introduction, php, php, beginners
advertisement



Introduction To PHP - PHP for Beginners!

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com