(adsbygoogle = window.adsbygoogle || []).push({});
PHP: Hypertext Processor
Welcome to this series of tutorials for PHP! These tutorials will be written by me, but will sometimes include codes of other programmers. When I do that, I will - of course - leave a source. If you have suggetions, corrections or have a tutorial you've written about the fine fundamentals of PHP, don't hesitate to PM them to me.
You must login or register to view this content. Is something unclear for you? Post a message in this thread and I'll try to explain it for you.
Now you know that, we can go on to the next step:
Getting Started.
[multipage=Getting Started]
PHP: Hypertext Processor
Getting Started
To start using PHP, you will need a few things. Here's a list of what you will need.
- Hosting with PHP5+
- Notepad (Microsoft Word = AIDS)
- A brain
- Concentration
1. Hosting with PHP5+
I can't stress this enough. If you don't have (hosting with) PHP, you can't proceed. If you think you can proceed. Please take a gun and shoot yourself.
2. Notepad
I recommend
You must login or register to view this content. for this. You may need to get used to it, but you'll enjoy it when you've mastered it.
For Mac OSX users, I recommend Smultron. You have to search it yourself, sorry.
3. Basic knowledge of HTML4.1+
This will come in handy, as I will use HTML as layout for our applications. I will not use CSS for layout. It is about PHP, not web designing!
4. A brain
Unlike Visual Basic, object-orientated programming languages like C++ and even PHP require a brain!
5. Concentration
Concentration is required (unlike Visual Basic). However, if you are diagnosed with disorders like ADHD, I think you can proceed anyway. Just make sure it isn't a critical version of ADHD.
this is not intended to discriminate people with ADHD
So you have that all? Let's get to write the first things!
[multipage=Your First App]
PHP: Hypertext Processor
Your First App
Let's make our first application. It's going to be very basic, so you should be able to follow right away.
When using HTML to say something basic like "Hello World!" You would use something like this.
<html>
<head><title>Hello World!</title></head>
<body>
<p>
Hello World!
</p>
</body>
</html>
Let's dynamify that with PHP!
<html>
<head><title>Hello World!</title></head>
<body>
<?php
//comments can be made with two slashes and then your comment
echo "Hello World!";
?>
</body>
</html>
This will make it do the same thing as you did with the HTML code we've looked at first. You might ask: "Kroxldyphivc, WTF is that good for?"
Well, it is something to start with!
You start a PHP-script with the following tags.
Opening tag: <?php
Closing tag: ?>
Back in PHP4 you could open and close tags with <? and ?>. But in the futuristic PHP6, this will be unsupported, rendering your code useless.
The command echo will display text to the screen. It is a special command, due to it not using () for the input, just like in other commands (ex. require("page2.php");). Keep in mind PHP frameworks like VoicePHP will replace this function with a voice.
You can also use print to display your text, but I disrecommend it due to it making the page load slightly longer (1/10 second). Again, the syntax for this function is print "Hello World!";. Basically the same as for echo.
Just like in C++, commands will be closed with a ; (sorry, forgot english name). This will tell PHP it can start executing the next command.
Could you follow? If yes, great! We will continue with variables! If not, it doesn't matter, try reading it again and you'll get it. Don't forget you can always ask your questions to me and I will answer them ASAP.
[multipage=Variables]
PHP: Hypertext Processor
Variables
Variables. Do you know a programming language without them? Well, PHP has variables. Let me explain what they are.
Variables include information. They have names. For example: $age will most likely include information about the user's age. It can include anything as value, from "newfag" to "oldfag", from 13 to 65 (numbers don't need quotation marks, you can just enter the number as value). As you saw, it can be really anything. The information in variables, can later be used in an application. Like maybe age restrictions, etcetera. One of the most used functions for variables is information an user sends through a form to the back-end PHP script.
Important to know!
- ALL variables will start with a dollarsign ($).
- Variablenames can have an unlimited long size.
- Variablenames can only exist out letters, numbers and underscores.
- Variablenames can only begin with a letter or an underscore. They can never begin with a number.
- Variablenames can not use spaces. As replacement for spaces, $my var will be $my_var or $myVar.
- There will be made a difference between capital and normal letters. $var is not the same as $Var.
- Give variables a clear name. Give them an one-word description about what it will be containing. If you're teaming up with someone to build an webapp, it will not be easy for your partner to understand what you're meaning with variables like $A, $B, $C and/or $D. PHP doesn't get confused by it, but you will get confused by it.
- Variables can contain ANYTHING. There are no restrictions in the values. The only restrication is; NO QUOTATIONMARKS! The only time you'll be using those are when you will be opening and closing the definition of your variable.
- PHP variables in general perspective can be defined after you use it. Example: <?php echo $username; $username = "Kroxldyphivc"; This will print Kroxldyphivc to the screen.
- You don't have to define what data type you're using for your variable.
Let's make an application that stores the name and age of an user inside the script (pre-defined) and make it output it to the screen/webpage.
<html>
<body>
<?php
$name = "Henk"; //var's with text or text + numbers REQUIRE to be defined using quotation marks!
$age = 16; //numerial var's can be defined without quotation marks. with quotation marks is also allowed.
echo "$name is $age years old."; //output to webpage
?>
</body>
</html>
The output will look like this:
Henk is 16 years old.
When outputting a single variable, you do the following.
<?php
$variable = "definition";
echo $variable;
?>
The same applies for the command print.
This is currently the last chapter. I am currently busy writing the new chapter, which will be covering string variables.
~Kroxldyphivc
29/03/2011