Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] Kicking Off With The Basics

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!

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 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!
Output
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:
PHP:
<?php
    echo 'Hello World';
?>
Outputs:
Code:
Hello World
Syntax & The Gist of Programming
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).
Variables
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.

  • String: Any kind of text, HTML, JS, XML, SQL, etc.
  • Int: Integer; A Positive or Negative Number.
  • Bool: Boolean; True/False; 1/0.
To declare a variable in PHP, you start by writing a dollar sign ($), and the name of the variable. Then, we use the equal sign to assign data of any type to that variable. The variable name can be anything as long as it:

  1. Starts with a dollar sign, followed by an allowed character that is not a number.
  2. Contains only: letters, numbers and underscores. (no spaces)
The value can be just about anything too, so long as it follows the basic Syntax Rules.

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)
?>
Outputs,
Code:
variable value
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...
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)
?>
Outputs,
Code:
0
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:
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)
?>
Outputs,
Code:
20
With integers you can perform math. That's a big reason why integers are so special. See this simple example,

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)
 ?>
Outputs,
Code:
-5

If you need additional help getting started with PHP, please view the desired parts of the Resources Thread in Coder's Paradise on RageZone Forums. :thumbup:


Credits: caja, LifeTaker, Parker
 
Last edited:
Ginger by design.
Loyal Member
Joined
Feb 15, 2007
Messages
2,340
Reaction score
653
Also reading the entire may be a little dry but it'll definitely help as they have useful code snippets to demonstrate each feature.
 
[R8]ℓσℓ32
Loyal Member
Joined
Oct 6, 2008
Messages
1,396
Reaction score
198
Two PHP Tags more:

1.- This is the same tag that it's used in javascript.
<SCRIPT LANGUAJE = 'php'>
Code:
 <SCRIPT>

2.- And this is using in ASP.
<% [Code] %>
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
caja said:
1.- This is the same tag that it's used in javascript.
<SCRIPT LANGUAJE = 'php'>
Code:
 <SCRIPT>
[/quote]
Yep, though nobody really uses the <script> tag for PHP..
[quote="caja"]
2.- And this is using in ASP.
<% [Code] %> 
[/quote]
What does ASP have to do with this topic?
 
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
What he means is you can use asp-style tags <% ... %> for PHP too, to use with editors such as frontpage who don't support the <?php ?> style tags (wonder why :rolleyes:)
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
What he means is you can use asp-style tags <% ... %> for PHP too, to use with editors such as frontpage who don't support the <?php ?> style tags (wonder why :rolleyes:)
Ooh, okay, So ASP tags do work with PHP. I see, that's cool. Is there any special server settings or PHP version, or is it always like that?

- Learn something new everyday! :8:
 
Master Summoner
Joined
Jan 11, 2009
Messages
505
Reaction score
8
<? ?> tags are the standard tags for asp. :p
 
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
Ooh, okay, So ASP tags do work with PHP. I see, that's cool. Is there any special server settings or PHP version, or is it always like that?

- Learn something new everyday! :8:

Lol you should've read your own spoiler ^^
Sometimes your PHP settings might have 'short_open_tag'(<? ... ?>), or 'asp_tag'(<% ... %>)
 

Zen

Custom Title Activated
Loyal Member
Joined
Dec 2, 2006
Messages
1,621
Reaction score
152
technically, yes, they work, but the good practice way to do it is with full php tags (<?php ?>)

they are recognised everywhere and when you're opening and closing alot of them within a single file they are easier to spot than <? or <% which the eye can commonly mistake for a html tag.
 
Newbie Spellweaver
Joined
Nov 2, 2009
Messages
54
Reaction score
21
technically, yes, they work, but the good practice way to do it is with full php tags (<?php ?>)

they are recognised everywhere and when you're opening and closing alot of them within a single file they are easier to spot than <? or <% which the eye can commonly mistake for a html tag.

Indeed. Besides <? ... ?> <% ... %> work only if short-open tags are enabled in php.ini. So I would advise using <?php ... ?> too, for cross-server compatibility reasons.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Lol you should've read your own spoiler ^^
Right, I edited it and corrected it since then. :8: Gotta keep it updated, you know. Did you notice the credits ;)

Yeah, I definitely suggest using '<?php' ... '?>'
Their easier than '<script>' tags, and way more cross-compatible than short/asp tags.



:w00t: Compatibility Rocks!

Sorry Caja, I didn't know there were asp tags before. You're #1 for contributing ;)
 
Last edited:
Praise the Sun!
Loyal Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
The ASP short-tags will be removed in PHP 6 and are deprecated somewhere in PHP 5.

-edit-

Wooooooooooooh, bump.
 
Back
Top