Cameron Garnett/Testpage7

From WikiEducator
Jump to: navigation, search
PHP

Super Quick PHP Tutorial

Introduction | Basic Syntax | Sending Data to the Browser | Comments | Variables | Strings | Functions | Quotes | Debugging | Form


VmvIcon Objectives.png

Learning Objectives

By the end of this page you will know how to declare variables using php code.

VARIABLES

Variable's in PHP are very tricky because the are case-sensitive and must be named properly. There are certain rules that apply when you are naming your variables and they are as follows:

Rules for PHP variables

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($topic and $TOPIC are two different variables)

Output Variables

The PHP echo statement is often used to output data to the screen.


VmvIcon Example.png

Example

<?php
$fun = "Wikieducator";
$pet = "dog";
echo "$fun rules!";
echo "my $pet is 2 years old";
?>

When you assign a text value to a variable put quotes around the value. In the above example, once i execute the code the variable (fun) has a value of "Wikieducator" and the variable (pet) has a value of "dog".

In this next example i will show you how to add two variables together and then output the sum to the browser.

VmvIcon Example.png

Example

<?php
$c = 30;
$a = 10;

echo $c + $a;
?>

the variable (c) has a value of 30 and the variable (b) has a value of 10. Because the value of these variables are numbers they do not need quotation marks.


VmvIcon Objective.png

ACTIVITY

write the above block of code into notepad ++ and get yourself familiar with creating variables.


Variable Scope

PHP variables have different scopes.

  • Global
  • Local

A variable that is created outside of a function() has a Global scope and can only be accessed outside of a function

VmvIcon Example.png

Example

<?php
$today = "monday"; // Global Scope

function calendar() {
//using the variable inside the function will generate an error message
echo "<p> the variable today inside of the function is $today</p>";
}

calendar();
echo "<p> the variable today outside of the function is: $today</p>";
?>

A variable that is created Inside of a function() has a Local scope and can only be accessed inside of a function

VmvIcon Example.png

Example

<?php
function calendar() {
$today = "monday"; // local Scope
echo "<p> the variable today inside of the function is $today</p>";
}

calendar();

//the variable outside the function will generate an error message
echo "<p> the variable today outside of the function is: $today</p>";
?>


VmvIcon Objective.png

ACTIVITY

write the above block of code into notepad ++ and see how the variables work.

Global Keyword

The Global Keyword is used to access a Global variable from within a function. This is done by writing the word Global before the variables inside your function.

VmvIcon Example.png

Example

<?php
$c = 30;
$a = 10;

function calendar() {
global $c, $a;
$c = $a + $c;
}

calendar();
echo "<p> todays temperature was $c</p>";
?>


VmvIcon Objective.png

ACTIVITY

write the above block of code into notepad ++ and see how the Global keyword is used to access the variables.

Static Keyword

Most of the time when a function has been executed the variables are deleted and no longer exist. Sometimes we want the variables to remain so that we can use them again if we are using the same function.
This is done by writing the word static before you create the variable withing the function.


VmvIcon Example.png

Example

<?php
function calender();
static $c = 30;
echo $c;
$c++;
}

calender();
calender();
calender();


VmvIcon Objective.png

ACTIVITY

write the above block of code into notepad ++ and see how the staic keyword is used to reuse the variables.