PHP Basics

What Does php stand for?

PHP: Hypertext Preprocessor

php tag

<?php
/ code goes here ?
?>

Comments in php

/ comments go here /
// or comments go here

Variables in PHP

&Variale_Name
– variables are dynamicaly typed (there are no compiled types)
– all variables start with $
$x = 16;
Variable Scope
Local – defined in a function
Global – any variable defined outside of a funciton is a global varible
Static – static $Remeber_me – varibale defined within a fuction that is not deleted when the function is complete, but the variable is only avalible to that function.
Parameter – Local variable whose value is passed to the function

Strings in PHP

$txt = “hello world”
strings are concatanated with the (.) i.e. $text_name = $txt . “Aaron”;
strlen($string) – givs the length of the string
strpos($string,$string_to_find) – returns the start index of $string_to_find if $string contains $string_to_find or else it returns false

Arithmetic operations in PHP

assignment operator is =
all common operators work for numbers +,-,,/,%,+=,-=,/=,=,++,–

Comparison operatons

== is Equal
=== is Identical (are equal and of the same type)
!= Not Equal
<> Not Equal
!== Not Identical
>,<,>=,<= all work in PHP

Logical Operators

&&, and are and operators
||, or are or operators
! not operator
xor xor operator

Conditional Statements

if statement – if (conditions){}elseif{}else{}
switch statement –
switch(n){
case n1:
/*code to execute if n == n1;
break;
default:
/code to execute if n is different from all of the cases/
break;
}

Tapes of Arrays

Numeric array – An array with numeric index
Associative array – An array where each ID key is associated with a value / like a dictionary /
Multidimensional array – An array contain one or more arrays

Creating an array

Two Methods
With array() – $cars = array(“Saab”,”Volvo”,”BMW”); or &cars = array(“Bob”=>32,”Jimmy”=>23);
Direct Define – $cars[0] = “Volvo”; or $cars[‘Peter’] = “32”;

Array Operators

Union – $x + $y;
Equality – $x == $y;
Identical – $x === $y; (True if x and y have the same key values pairs and order)
Inequality – $x != $y; or $x <> $y;
not Identical – $x !== $y;

While Loop

while(condition)
{
// code to be executed
}

DoWhile Loop

do
{
// code to be executed at least once
}
while (condition)

For Loop

for (init; condition; increment)
{
// code to be executed
}

For Each

// used to enumerate an array
foreach ($array as $value)
{
// the value of the current array is stored in $value
}

Defining a function syntax

function functionName ($arg1, $arg2, …)
{
// do function stuff here
return $values; // $values are returned
}

$_GET variable

// the $_GET variable may be used by a php script that was called using the get method
// the key value store is embedded in the url as follows www.google.com/run.php?search=12&age=12
// the values are accessed as follows $_GET[‘search’]; // returns 12

$_POST variable

// May be used by a php script when the method = “post’ was used when the script was called
// the key/values store can be accessed by the script as follows $_POST[‘Var’];

Forms using php scripts

//HTML forms can use action=”script.php” and method=”post” or method=”get” to pass data to a php script
//if so the $_GET or $_POST variables will be available in the script

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*