alert($_COOKIE['cookie']);"; } ?>
banner

PHP Tutorial

This is the PHP tutorial page.

Here is my basic PHP tutorial, throughout this tutorial we will give you a basic list of php commands, and a quick walkthrough. Php is an OOP language, which means you can create objects with the use of encapsulation (public/private variables). There are many more capabilities of the PHP language. For more info go to Php.net

Lets get started, all php files must have the .php extension to work properly.

Here are some basic PHP Commands

    Basics



    Printing Text

  • print - Displays text on the page
  • printf - Displays text on the page
  • echo - Displays text on the page
  • Variable Creation

  • $variableName - Tells the interpreter this is a variable
  • Operators

  • + addition
  • ++ increment by 1
  • - subtraction
  • -- de increment by 1
  • * multiplication
  • / division
  • % modulous
  • Comparison Operators

  • == equal to
  • === strict equal to
  • != not equal to
  • <= less than or equal to
  • >= greater than or equal to
  • > greater than
  • < less than
  • Conditional Statements

  • if - only executes if a specific condition is true
  • if..else - executes if a specific condition is true, if its not then it executes the next block of code
  • if..else if..else - executes if a specific condition is true, if it's not then it checks the next statement and executes if that condition is true, if it is not then it executes the last block of code
  • switch - gives options to the user to execute a specifec block of code. Almost like a choose your own adventure book
  • Functions

  • function anyNameHere(){} - used to declare a function
  • String Functions and Operators

  • str_word_count(string) - counts the number of words in a string
  • strcspn(string1, string2) - returns the number of characters in string 2 that do not match string 1
  • strlen(string) - returns the number of characters in a string
  • substr_count(string, substring) - returns the number of occurances of a substring
  • stripos(string, search string, start position) - searches through a string and returns the first position of one string in another
  • substr(string, start position, length) - returns the position of a string
  • explode(where to break the array, arrayName) - takes an array and breaks it up at a specified marker
  • implode(where to combine the array, arrayName) - takes an array and puts it into a single string
  • I will not go into str_replace, substr_replace, etc.. These are known holes for XSS
  • Loops

  • for - executes a specific, defined number of times
  • while - executes while a specific condition is not true
  • Date and Time

  • date(m-d-Y) - displays the date
  • time(hh:mm:ss) - displays the current server time
  • Get Environment Variable

  • getenv(options) - gets environment variable of specified option
  • Getenv options

  • getenv('REMOTE_ADDR') - gets the clients ip address
  • getenv('DOCUMENT_ROOT) - gets the source documents root directory
  • getenv('SERVER_ADMIN) - gets the server administrators email address
  • getenv('HOSTNAME') - gets the servers hostname
  • getenv('REMOTE_USER') - gets the clients username for logging purposes
  • getenv('USERNAME') - logs the users windows username
  • Handling Form Data

  • $_POST['variable'] - Retrieves the posted form data
  • $_GET['variable'] - Retrieves the posted form data
  • isset() - checks if the form data is empty
  • empty() - checks if the form data is empty
  • is_numeric() - checks to see if the form data is numeric
  • File Streams

  • fopen(file_name, mode) - opens a file for reading or writing
  • fclose() - closes the file
  • File Stream Modes

  • a and a+ - append to the end of the file
  • r and r+ - read only
  • w and w+ - opens the file for writing
  • x and x+ - creates and opens a file for writing
  • Reading and Writing to Files

  • fwrite() - writes to a file
  • file_put_contents(filename, string) - writes a string to a file
  • is_dir() - checks to see if the specified directory exists
  • file_get_contents(filename, include path) - reads contents of a file into a string
  • fread(filename, length) - reads a file into a string of specified length
  • readfile(filename, path) - prints the contents of a file
  • Databases

  • mysqli_connect(host, user, pass) - connects to a database
  • mysqli_select_db(mysqli_connect, databaseName) - seelects a database
  • mysqli_close(database name)
  • mysqli_query(mysqli_connect, query string) - queries the database
  • mysqli_fetch_row() - fetches the data in a row of a database table
  • I will not go into Sql, or MySql queries in this tutorial
  • Cookies and Sessions

  • setcookie(name[value, expires, path, domain, secure]) - creates a cookie
  • $_COOKIE['part of cookie to read"] - reads the specified part of the cookie
  • session_start() - starts the session
  • seesion_id() - retrieves the current session id
  • $_SESSION[] - access to the seesion information
  • session_destroy() - ends/deletes the current session


  • [Home] [page1-HTML] [page2-CSS] [page4-JavaScript-Part1] [page4-JavaScript-Part2]

First you have to use the following tags like so:

<?php
This is where the actual code goes
?>


Let's start by writing simple text to the screen

Open your text editor, or crimson editor, create a new document and save it as "first.php"
The basic structure to write text to the screen are as follows:


<?php
print("this is a statement written in php");
?>

Notice that the print command is followed by (" and ends with the opposite ")
just like javascript.


Now we will learn how to create functions

Let's see what a function looks like, open the previous page we created if it's not already open.
Change the document to look like the following example.

<?php
function writeText()
{
print("This is a statement written in php");
}

writeText();

?>

Notice that the function is accessed by php using the name of the function followed by a semicolon. Creating the function is essentially the same as javascript except there is no script tags, only php tags.

NOTE: You can use the print statement to write html to the screen with a little extra code
print("<img src='someImage here! />");


Now that we know how to declare a function, let's move on!

Now we will declare variables, and access them

Start by adding the following code to the document:

<?php
function writeText()
{
$myText = print("This is a statement written in php");
$myVar = 1;
$myVar2 = 2;
print($myText . $myVar + $myVar2);
}

writeText();

?>

We have now changed the function so that when it prints it will take the statement "This is a statement written in php", and concatenate(add the text to it from the other two variables) with the use of the period. This is how we combine text and variables in php. The next statement $myVar + $myVar2 will add the values of these variables together. So the statement will say "This is a statement written in php3"

Now that we know how to create variables let's move on.

Now we will create a cookie.

<?php
setcookie("myCookie", "Ghost");
print $_COOKIE['myCookie'];
?>

This will create a cookie that can be accessed by calling the name "cookie", this statement will print out the word "Ghost". All options following the name of the cookie are optional and are the actual areas that store the data. Sessions are similar except they are only valid until the session is terminated. I would recommend using sessions for user authentication instead of using cookies. Cookies can be stolen while transmitting data across the web. Now lets move on to the next section.