PHP

PHP Menu

Naming Conventions

  • variables start with a $ sign, followed by the name of the variable.
  • variable names must start with a letter or the underscore character _.
  • variable names cannot start with a number.
  • variable names can only contain alpha-numeric characters and underscores (A-z, 0-9, and _).
  • variable names cannot contain spaces.

Here are some examples:

<?php
$txt = "Hello world!"; $myNumber = 5; $myFloat = 10.5; echo $txt . "<br>"; echo $myNumber . "<br>"; echo $myFloat . "<br>";

Scope of Variables

Variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. There are three different variable scopes in PHP:

  • local
  • global
  • static

Global Scope

Variables declared outside a function have global scope and can only be accessed outside a function. Below is an example:

<?php
$myGlobal = 5; // global scope
function myFunction() { // using myGlobal inside this function will generate an error echo "<p>Variable myGlobal inside function is: $myGlobal </p>"; echo "<br>"; }
myFunction(); echo "<p>Variable myGlobal outside function is: $myGlobal </p>"; echo "<br>";

Local Scope

Variables declared within a function have local scope and can only be accessed within that function: Below is an example:

<?php
function myFunction() { $myLocal = 5; // local scope echo "<p>Variable myLocal inside function is: $myLocal</p>"; echo "<br>"; }
myFunction();
// using myLocal outside the function will generate an error echo "<p>Variable myLocal outside function is: $myLocal</p>"; echo "<br>";

The global keyword

The global keyword is used to access a global variable from within a function. Below is an example on how the global keyword can be used.

<?php
$myFirstNum = 5; $mySecondNum = 10;
function myFunction() { global $myFirstNum, $mySecondNum; $mySecondNum = $myFirstNum + $mySecondNum; }
myFunction(); echo $mySecondNum; // outputs 15

The static keyword

After a function is executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. To do this, use the static keyword when first declaring the variable.

<?php
function myFunction() { static $myLocal = 0; echo $myLocal; echo "<br>"; $myLocal++; }
myFunction(); myFunction(); myFunction();

Each time the function(myFunction()) is called, that variable($myLocal) will still have the information it contained from the last time the function was called. Note that the variable($myLocal) is still local to the function.

Exercise

Create a variable named $myVar. Assign the value 1201 to the variable. Print the value of the variable.

<?php
$var = 123;
<?php
$myVar = 1201; echo $myVar;
{ "test_output_contains":{ "expected":"1201", "error_message":"Did you assign the correct value for <code>$myVar<\/code>?" }, "test_variable_exists":{ "object":"$myVar", "error_message":"Have you declared <code>$myVar<\/code>?" }, "success_message":"Good job!", "error_message":"Please read the instructions again." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods