Help - Search - Members - Calendar
Full Version: Learning the Basics of PHP
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
albinoAZN
PHP Introduction:

PHP is a computer language for the world wide web. You may think want PHP
stands, but actually it does not stand for anything
unlike many other computer languages.

PHP Editors:

There are many php editors out on the market, some free, some paid. A very
popular editor is Dreamweaver, but you have to buy it first. I use
PHP Eclipse, which is a plugin for Eclipse, which is an Open Source software.
But for this tutorial all you will need is notepad.

How-To open a PHP document:

To open a php document or to start php all you have to type is:

CODE
<?php


Or:

CODE
<?


To close a PHP document is to type:

CODE
?>


In between these 2 tags you can then execute your PHP code.

Print text to the browser:

To print some text to the browser you have to do the following:

CODE
<?php

echo 'Hello World'; //This will print 'Hello World' to the browser.

?>


Now the echo command is telling PHP to print the following text to the browser.
Notice how I have a ';' at the end
of the end. If you do not add this, then you will not be able to execute any
more PHP code or you will get an error.
This is telling php that this line of code is finished, and you then want to go
onto the next line of code in your document.

Saving a PHP Document:

When saving a php document, you want the extension to be '.php' eg:
'my_first_app.php'
You will then need a server that supports PHP to view your the document you
just created.

Comments:

Comments are so you can add a comment in your php document.

If you type '//' (without quotes) this will then comment anything you type on
the current line, like what i have done in the example above.
If you wish to comment more than 1 line, then you will have to type: '/*' to
open it, and '*/' to end the comment.

Example:

CODE
<?php

/* Start the comment

This is a multiline comment.

More text.

End the comment here */

echo 'Comment Finished'; //Comment that will only work on the current line.

?>


Variables:

Variables is something that will hold data for you. The later you can use the
variable later in your code.

Example:

CODE
<?php

$var = 'This is a variable'; //Store 'This is a variable' in the varaible
called: '$var'

echo $var; //This will print what ever is stored in $var.

?>


If Else Statements:

If else statments are one of the most important features in PHP aswell as a lot
of languages. It allows for conditional execution of code.

Example:

CODE
<?php

$num1 = '1'; //Store some data
$num2 = '2'; //Store some more data

if ($num1 == $num2) //Make sure you type '==', this will then check if $num1 =
$num2. If you just put '=', Then $num1 will equal $num2
{ //Start the code to execute
echo 'Success';
} //End the code that you executes

else //If $num1 does not equal $num2 then we want the execute the following
code:
{
echo 'Failed';
}

?>


written by my good friend ben to go on my website, But I shut down on submitting tutorials when he gave it to me.
coldkill
Nice into to PHP. However, PHP stands for Hypertext PreProcessor (HPP really instead of PHP but meh). It used to be the creaters initials I believe.

CODE
<?PHP

Is a better way of starting off PHP code because some early engines may get confused and not start the script dunno why but better to be safe than sorry.

You can also use the
CODE
#

symbol to start single line comments.

Variables can also be intialised by using the function var then the name with a $ sign the static data ( you can't declare it with another variable.
Example
CODE
// Works
var $var = "This is a variable!";

// doesn't work
var $var = $another_var;


Cold
Minh88BT
^^ i still dont get it victory.gif
Indigo
Really, really basic, but it might come in handy for somebody. You should've included something about loops and use of "else if", then it would've been better.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.