An introduction to PHP
What is so great about PHP? What can you do with it, and how does it work? Read this tutorial to begin learning PHP.
Hello!
This is an introduction to PHP and how some basic things work.
First, php is a server-side scripting language, which means that everything you script in php is processed by the server, before it goes out on the page. If you look on a .php website, and check the sourcecode, you'll see that there's only HTML there. Well, that's just because the server already processed all the php in the script, and echoed out the things that the scripter wanted the script to echo out.
If you define a variable in a php-script, and put up the script on a website, there is no way to actually see the variable in the sourcecode, unless they get the .php file from the webhotel.
So what does that mean? It means for example, that you can make simple login system that are still very secure.
These are the opening tags that tells the server, "Wait, now it's php. Ahh! We have to process it!".
Also, I might add that to actually test php scripts, you will need a webserver of some kind. If it's hosted on your computer, or on a webhotel doesn't matter as long as it has php installed.
Variables:
So, I mentioned variables before, what are they?
Variables are kinda like a storage. You can add a value to a variable, and it will forever hold that value throughout the script. Let's take a simple example.
Note: I'm not gonna write the php opening tags in all the examples, but in order for the script to actually work, they have to be there around everything.
PHP Code:
echo 'Hello world';
So what did we do there? Well, I told the server that I wanted it to write that out on the page. Everything you "echo" out, is written on the page, like in regular HTML. If we were to check the sourcecode on that page with that code, it would only be "Hello world" because the other things were processed by the server.
Back to variables. Variables are defined with a $-sign before the name of the variable. That way the server knows it's a variable, the name doesn't really matter.
But with names it's important to know, that you cannot start a variablename with a number.
And why would we need variables you ask?
PHP Code:
$hello1 = 'Hello world';
$hello2 = 'Hi there!';
echo $hello1;
echo '<br />';
echo $hellor2;
Whoa! Now alot of stuff happend there!
First off, we created 2 variables, called hello1, and hello2. We assigned different values to those. The first one got the value "Hello world", and the second one got "Hi there!".
After that, we simply echoed those out on the page. This code would generate this:
Hello world
Hi there!
Pretty basic script, doesn't really fill any function. But it's important that you understand the meaning of variables.
PHP Tips: A variable is also known as a "var".
And here's a more advanced example of the usage of variables:
PHP Code:
$var1 = 'Tanax';
$var2 = 18; // <-- Note that we don't have to use ' around numbers, because that value is not a string, it's an integer. Also, comments are defined with 2 slashes.
$var3 = 'Sweden';
echo 'Hello, my name is ' . $var1 . ', I am ' . $var2 . ' years old, and I live in ' . $var3;
I'm pretty sure you understood the part where I defined the variables and assigned the values. But did you get the hang of the echoing out part?
Let's take a look.
First, this is a string. A string is a.. phrase. It can contain numbers, but it cannot ONLY be a number, because that is an integer.
So what's with the dots? Well, if you're going to echo out variables inside a string, in the same echo, you have to do it like that.
'string ' . var . ' string';
If we have strings on both sides, we have to have dots on both sides.
'string ' . var;
If we have a string on only one side, we only need one dot, and also, you can end the echo right there after the var.
var . ' string';
Pretty self-explenational.
If you tried this at home now, and you wrote something else than me, you might've gotten error messages. You see, if you write a ' inside a string, it ends the string.
PHP Code:
echo 'Hello, I'm 18 years old so you're not old enough';
This wouldn't work. I'm using ' inside the string(you're and I'm), and that means that the string ends, and I have to use a ; after a string ends. Which I'm not after you'.
So how do we get around this?
Well, you have to escape them.
PHP Code:
echo 'Hello, I\'m 18 years old so you\'re not old enough';
This would work.
And this was the end of this intro lesson to php.
We've discussed the usage of variables, and how to echo out things on a page.