PHP Basics

From JumbaWiki

Jump to: navigation, search

Here are some core basics to get your started with PHP. For more information, have a look at the PHP Code Snippets.

Contents

Hello World

For more information, see main article: Hello World

What would an intro to a programming language be without a "Hello World" script! In this example we will get PHP to print "Hello World".

  • Create a file called helloworld.php and insert the following code:
<?php
    echo "Hello World";
?>

It's that simple. When you visit the page: http://www.yourdomain.com/helloworld.php you should see "Hello World" printed on the page.

Viewing information about PHP

For more information, see main article: PHP Info

Now you can start getting technical and create a page that displays all the information about PHP installed on your server. This can be handy if you are starting out as you are able to see what is installed module wise, as well as seeing what some of the "predefined variables" are. See the main article for instructions and further details.

Getting started with PHP

Here is a basic starting point to learning some of the fundemental elements of PHP.

Initial syntax

PHP code always starts with <? and ends with ?>. It is advisable to start with <?php and also sometimes with the PHP version number <?php3.

Variables

Variables are written with a dollar sign.

    $myVar = "hello";

Output

To spit something out the other end you can use echo.

//output from a text string
echo "Hello World";
 
//output from a variable value
$myVar = "Hello World!";
echo $myVar;
 
//output from a function
function spitItOut(){
  return "Hello World";
}
 
echo spitItOut();
 
//output from a variable passed to a function
function spitItOut(myVar){
  echo $myVar;
}
 
spitItOut("Hello World");

Boolean/If Statements

Functions

Loops

Arrays

See also

External links


Code Basics
ActionScript | ASP | ASP.NET | CGI | ColdFusion | CSS | HTML | Java | JavaScript | JSP | MySQL | Perl | PHP | XML | XHTML
Personal tools
Australian Photographs | Unique Views of Reality | Daedalus