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 (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 ?> // <?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:
// 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!).
$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):
// 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.:
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):
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.