- Joined
- Jun 8, 2007
- Messages
- 1,985
- Reaction score
- 490
This is a tutorial for anyone interested in learning the basics of PHP. Mostly, an introduction to the syntax, and common good practices used in the PHP Scripting Language.
Be sure to read all of the Spoilers!
Be sure to read all of the Spoilers!
What & How's
PHP is a Server-Side Scripting Language used mostly for Web-Development for Dynamic Web-Sites. PHP is a very versatile language, capable of many things. I'm mostly going to be talking about the PHP 5 series, but the scripts may work in other versions as well (especially in tutorials like these). To begin development in PHP, you need a HOST or a server compatible to run PHP. Such as WAMP or even the LAMP server. There are others. WAMP is for Windows, LAMP is for Linux. Just get an Apache server, or a host. Either one will do. (I'd suggest a host, or both). To begin writing PHP scripts, just create a document with a '.php' extension in your server or host. Such as 'hello_world.php'. Then, we use PHP Tags to enclose our scripts...
PHP Tags (<?php ... ?>) are used so that the server can pick out the PHP as opposed to HTML and other scripts/markup. In these tags, we put every PHP script. Sometimes your PHP settings might have 'short_open_tag'(<? ... ?>), or 'asp_tag'(<% ... %>) enabled. Either way, for compatibility between servers, you should not use 'short_open_tag', or 'asp_tag' in your applications (assuming you want the script to work on other servers). I only use the tags, (<?php ... ?>). Also, the server may get confused with XML because it also uses the question mark(?) in the tags.. (Though there are ways to avoid that, don't use 'short_tags'.)
You can also call to PHP in <script> tags(<script language="PHP"> ... </script>).
One of the cool things about PHP tags, when a PHP page is requested (by a client; or the end-user on their browser), the server reads the PHP tags and hides them before the page gets sent to the client. That's good for security, and instant compatibility between all the browsers. PHP is written in the mark-up, so it's easy to code right inside your web-site, but cannot be seen by the client, so it's by default, secure!
You can also call to PHP in <script> tags(<script language="PHP"> ... </script>).
One of the cool things about PHP tags, when a PHP page is requested (by a client; or the end-user on their browser), the server reads the PHP tags and hides them before the page gets sent to the client. That's good for security, and instant compatibility between all the browsers. PHP is written in the mark-up, so it's easy to code right inside your web-site, but cannot be seen by the client, so it's by default, secure!
I'll be using the term, 'output' frequently. Get to know that term. Anything that gets sent to the screen is output. That's, in a nutshell, what it means. Anytime we want to leave a message on the screen, place an image or video, or build a table of data, we just call it "sending output". Everything you see on your monitor is output in some language, PHP sends output too.
In PHP, we can send the text, 'Hello World', to output by using the 'echo' statement, like so:
Outputs:
In PHP, we can send the text, 'Hello World', to output by using the 'echo' statement, like so:
PHP:
<?php
echo 'Hello World';
?>
Code:
Hello World
Now, let's go through the syntax used in the above example.
First, we opened up a PHP tag. (<?php)
Second, we pressed tab, then wrote out an echo statement. (echo)
Third, after the echo statement, we put a 'string of text' to be output.
Fourth, we finished the statement with a semicolon (
Fifth, We closed our PHP tags (?>)
Every time you finish a statement, finish it with a semicolon(. That's simply how it's done. There are two, distinct kinds of code. There's the line of code, the statement; And the block of code, the code block. A statement starts with anything, and ends with a semicolon. The Code block, is surrounded by curly brackets.(Ex: { code block } ). Typically, a code block will contain two or more statements that go together. Additionally, it's very common for code blocks to contain other code blocks. (Such as a loop inside a conditional statement inside a function).
First, we opened up a PHP tag. (<?php)
Second, we pressed tab, then wrote out an echo statement. (echo)
Third, after the echo statement, we put a 'string of text' to be output.
Fourth, we finished the statement with a semicolon (
Fifth, We closed our PHP tags (?>)
Every time you finish a statement, finish it with a semicolon(. That's simply how it's done. There are two, distinct kinds of code. There's the line of code, the statement; And the block of code, the code block. A statement starts with anything, and ends with a semicolon. The Code block, is surrounded by curly brackets.(Ex: { code block } ). Typically, a code block will contain two or more statements that go together. Additionally, it's very common for code blocks to contain other code blocks. (Such as a loop inside a conditional statement inside a function).
The text, 'Hello World', is considered a string by PHP. Whenever you wrap text in apostrophes('), it's almost like declaring whatever it is as a string. Quotation marks are more like wild-cards. They look for variables, functions, and various other things (ints, booleans), along with strings. Because of that, apostrophes are the most efficient things to wrap around strings. So that's what I'll always be using.
There are other ways to assure whatever it is you're declaring is a certain type of variable. First, let's go through the basic types of variables, then how to declare them.
By specifically declaring the type for the value, PHP will skip some steps and concentrate on partially sanitizing certain things, like Ints and Bool variables, to make sure they're right. It's easier to explain it in an example
Outputs,
Now, if we try to do the same thing, except use (int) in place of (string),. we might get an unexpected result, but it actually makes sense...
Outputs,
The reason it outputs 0, is because an integer cannot contain any characters other than +/- numbers. So if it doesn't see any numbers at all, it will put a 0. That's what we call the "Null" value of an integer.
(With a (bool) type, the same result will occur, except the Boolean Null Value is 0 for false, instead of the number 0.)
The Correct way to declare an integer would be with a number, like so:
Outputs,
With integers you can perform math. That's a big reason why integers are so special. See this simple example,
Outputs,
There are other ways to assure whatever it is you're declaring is a certain type of variable. First, let's go through the basic types of variables, then how to declare them.
- String: Any kind of text, HTML, JS, XML, SQL, etc.
- Int: Integer; A Positive or Negative Number.
- Bool: Boolean; True/False; 1/0.
- Starts with a dollar sign, followed by an allowed character that is not a number.
- Contains only: letters, numbers and underscores. (no spaces)
By specifically declaring the type for the value, PHP will skip some steps and concentrate on partially sanitizing certain things, like Ints and Bool variables, to make sure they're right. It's easier to explain it in an example
PHP:
<?php
$variable_name = (string) 'variable value'; //A Statement Declaring a 'string type' Variable.
echo $variable_name; //Send the variable to output. (so we can see it)
?>
Code:
variable value
PHP:
<?php
$variable_name = (int) 'variable value'; //A Statement Declaring an 'int type' Variable, but with text.
echo $variable_name; //Send the variable to output. (so we can see it)
?>
Code:
0
(With a (bool) type, the same result will occur, except the Boolean Null Value is 0 for false, instead of the number 0.)
The Correct way to declare an integer would be with a number, like so:
PHP:
<?php
$int_var = (int) 20; //A Statement Declaring an 'int type' Variable, with the number 20.
echo $int_var; //Send the variable to output. (so we can see it)
?>
Code:
20
PHP:
<?php
$int_a = (int) 20; //A Statement Declaring an 'int type' Variable, with the number 20.
$int_b = (int) -25; //A Statement Declaring an 'int type' Variable, with the negative number -20.
echo ($int_a+$int_b); //Add the two integers, and send to output. (so we can see it)
?>
Code:
-5
Credits: caja, LifeTaker, Parker
Last edited: