Monday, November 7, 2011

PHP Basics

PHP : Hypertext Preprocessor (server side scripting/executed on the server)
-  Open source software, free to download and use
-  can contain text, HTML tags and scripts
-  PHP files are returned to browser as plain HTML
-  Allowed extensions ".php", ".php3", ".phtml"
-  If the file has .html extension, the PHP code will not be executed

Why PHP:
-  PHP combined with MySQL are cross platform (Windows/Linux/Unix/etc.)
-  compatible with almost all servers used today (Apache, IIS, etc.)
-  free (open source)
-  easy to learn and runs efficiently on server side

Where to get it :
PHP : http://www.php.net/downloads.php
MySQL : http://www.mysql.com/downloads/
Apache :  http://httpd.apache.org/download.cgi

Variables in PHP
-  a variable does not need to be declared before adding a value to it.
-  no data type declaration for the variable is needed
-  PHP automatically converts the variable to the correct data type, depending on its value
-  variable name must start with a letter or an underscore "_", 
-  variable name can only contain alpha-numeric characters and underscores
-  A variable name should not contain spaces.


$var_name = value;                   //declaring a variable

PHP open and close tag
<?php      ?>                           //PHP open and close tag

output text
echo "Hello World";              // output text with php

Comments
//this is comment
/*this is
comment */  

Strings
$txt="Hello World";
http://www.w3schools.com/php/php_ref_string.asp     (PHP String functions reference)

Concatenate Strings 
echo $txt1 . " " . $txt2;

Length of String 
echo strlen("Hello world!");               //output is 12


String Position
echo strpos("Hello world!","world");        //output is 6; starts from 0 not 1


Arithmetic Operators
+     //addition
-     //subtraction
*     //multiplication
/     //division
%     //modulus (division remainder)
++    //Increment
--    //Decrement


Assignment operator
x=y
x+=y     //x=x+y
x-=y     //x=x-y
x*=y     //x=x*y
x/=y     //x=x/y
x.=y     //x=x.y    
x%=y     //x=x%y


Comparison Operator
==       //equal to 
!=       //not equal
<>       //not equal
       //greater than
       //less than
>=       //greater than or equal to
<=       //less than or equal to


Logical Operators
&&       //and
||       //or
       //not


if...elseif....else
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d=="Sun")
  echo "Have a nice Sunday!";
else
  echo "Have a nice day!";


Switch... Case
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}

Array (Numeric Arrays)
A numeric array stores each array element with a numeric index.

$cars=array("Saab","Volvo","BMW","Toyota");
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";

Array functions reference
http://www.w3schools.com/php/php_ref_array.asp

Associative Arrays
An associative array, each ID key is associated with a value.
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

echo "Peter is " . $ages['Peter'] . " years old.";

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

Multidimensional Arrays
each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. 


$families = array
  (
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
  );

echo "Is " . $families['Griffin'][2] ." a part of the Griffin family?";

While Loop
while($i<=5)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }

Do...While statement
will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.
$i=1;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";   //first output line "The number is 2"
  }
while ($i<=5);

For Loop
for ($i=1; $i<=5; $i++)
  {
  echo "The number is " . $i . "<br />";
  }

Foreach Loop
$x=array("one","two","three");
foreach ($x as $value)
  {
  echo $value . "<br />";
  }

Functions
  • To keep the script from being executed when the page loads, you can put it into a function.
  • A function will be executed by a call to the function.
  • You may call a function from anywhere within a page.
  • The function name can start with a letter or underscore (not a number)
------------------------
function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}

echo "My sister's name is ";
writeName("Hege","!");
------------------------
function add($x,$y)
{
$total=$x+$y;
return $total;
}

echo "1 + 16 = " . add(1,16);               //output is 1 + 16 = 17
------------------------

HTML Forms
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

$_GET Variable
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.

http://www.w3schools.com/welcome.php?fname=Peter&age=37

Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
  • because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
  • The get method is not suitable for very large variable values. It should not be used with values exceeding 255 characters.


$_POST Variable
  • Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
  • there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file)
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

$_REQUEST Variable
  • The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
  • The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.

No comments:

Post a Comment