A group about Web Design & Development. Ask and Learn everything related to web design and development.

Report article RSS Feed PHP for total beginners! Part #1

PHP for total beginners. The conclusion of these tutorials will be a web-browser based game.

Posted by feillyne on Dec 6th, 2009
Basic Website.

PHP is a web language, using variables, functions and generally whole scripts to do its job of making html pages. PHP means PHP Hypertext Preprocessor. PHP is very often combined with MySQL databases to create various applications, e.g. web browser-based games (php defines player's moves while MySQL saves them to the database, the same with his stats and so on).

Do not try to bend the spoon - that's impossible. Instead, only try to realize the truth: there is no spoon.

The truth is PHP, it creates html pages which don't really exist. What does it mean? Have I gone mad?

You place .php files on the web server, then the server reads PHP (if it is capable of doing it), then sends the data to your browser.
That means that .php files don't create PHP files in your browser, but actually HTML pages. If you take a look at source code of a processed .php file (inside your web browser), you'll see only HTML tags.

No, you can't access PHP files - its direct source code - through your browser. That's how devs' work is kept safe from all prying eyes.

There is a difference between knowing the path and walking the path.

A big difference. Let's start learning PHP!

You need to get some programs simulating a web server (which supports PHP). There are various ones, you can use xampp if you don't mind (or any other):
Apachefriends.org
Apachefriends.org (for Windows users)

There are many PHP editors, but to be sincere, you need only a text editor (which has a PHP syntax highlighting function).
Run xampp. Go to the folder htdocs your xampp folder (e.g. C:/xampp/htdocs), create a new folder named phptut.
You can use MS Notepad or WordPad to create files in your new folder.

They must have extension .php, e.g. "start.php" (with quotes if you're using Notepad, this way it is saved as a .php file).
Let's create a start.php file.

Every .php file should start and end with a PHP opening and closing tag, namely:

php code:
<?php

?>

// <?php or <? must be used at the beginning of php file and ?> at the end.

There are special tags, which make the code invisible to the web server, i.e. ignored by the PHP interpreter. You can write there anything you want to. :-) You can't do the same without them, because it'll cause so-called parse errors. It'd be like giving a student not learning Chinese a Chinese textbook - he won't understand anything unless you'll explain it and give him anything needed to comprehend it.

Write (start.php file) between opening and closing tags:

php code:
// the first kind of comments, one-line only
/* the second sort of them
one can use them at the whole page
here too :-)
"/*" starts the comments and "*/
" ends them. */

PHP scripts consist mostly of variables (and other things, later). Variables can vary, they are containers for whatever you put into them. They can contain water, some small bricks, or anything else, which can be held by them.
You can call them as you want to, unlike html tags.

Let's create a few variables after the comments written above (but before the closing tag!).

php code:
$sword = "Rapier";
$sword_damage = 35;
$sword2 = "Long sword";
$sword2_damage = 40;

As you can see, a variable can be named as you like to name it. Notice that any words/phrases must be inserted between quotes (" "), such variables are called strings. Numbers can be assigned without any quotes (they are known as integers, in most cases).
Every PHP line should end with ";" character, unless you want to see a parse error.

To load up your webpage you should type in your browser address Localhost

Nothing shows up, eh?
It's because you didn't tell the browser to display anything. You just put some containers inside a closed room without windows or doors.

Use "echo" function to display anything (you can also use "print" function):

php code:
// Use "echo" function to display anything (you can also use "print" function):
echo $sword;
echo " does $sword_damage damage";
echo "<br /><br />"; // echoes html tags <br /> (<br>), which are interpreted as html code and break lines.

You can assign various functions to your variables. E.g.:

php code:
echo "The actual inflicted damage: ";
$damage_dealt = rand(1,$sword_damage); // rand is a random number function, it creates a random number. Here 1st one denotes the first/starting number, the second one is the maximal damage. You can injure your opponent only for 35 damage, which is the damage dealt by a rapier (defined earlier). ;-) You can also use rand(1,35)
echo $damage_dealt; // displays $damage_dealt, which isn't a string or a number, it is a function assigned to $damage_dealt, i.e. rand(1,$sword_damage) - a random number

A preview of the file (done in a text editor capable of highlighting PHP code):
Media.moddb.com

Exercises
Make some variables. Remember that they must start with a dollar sign ($), mustn't start with _ or a number or any white spaces. These containing "_" at the beginning are special variables, we'll talk about them later. (I.e. $1variable, $_variable, $ variable are all wrong.)

That's all for the first tutorial. :-) More soon. The final one will contain full instructions how to make a web-browser based text only game.

Post comment Comments
Vikestart
Vikestart Dec 7 2009, 7:14am says:

Well, ?> doesn't necessarily have to be at the end of a PHP file. You have have multiple <?php and ?> inputs in the file. For example some in the very top to load for example server settings like an automatic maintenance system or forum session input.

If you want a redirect, you can use this:

<?php
header('Location: Yoursite.com');
?>

It must be placed at the very top before the <HTML> tag

+2 votes     reply to comment
feillyne Author
feillyne Dec 7 2009, 7:28am replied:

Yup, that's true, one can have multiple <?php/<? ?> in the file. PHP can be inserted inside html pages, and so on.
I'm leaving it for include/require function tutorial. :-)

+1 vote   reply to comment
frvge
frvge Feb 15 2010, 10:45am replied:

Don't forget to exit or die directly after the header. Saves time ;)

+2 votes     reply to comment
frvge
frvge Feb 15 2010, 10:44am says:

Give me a sign when the next tutorial is up for publication. I can check it for security issues.

+2 votes     reply to comment
feillyne Author
feillyne Feb 16 2010, 8:14am replied:

Hmm, dunno when, but I'm going to post a tutorial how to make a RTS-styled map. ;-) At least not optimised one.

+1 vote   reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Established
Nov 13, 2007
Privacy
Public
Subscription
Open to all members
Contact
Send Message
Email
Members Only
Membership
Join this group
Group Watch
Track this group
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Groups
Inside the Page
Inside the Page Hobbies & Interests group with 54 members
Web devs & web game devs
Web devs & web game devs Hobbies & Interests group with 35 members