Week 7: Introduction to PHP

Introduction
Being only a web language, this lightweight & open source development language is a powerful one which is widely used throughout the world to develop web sites. Apart from being well documented, it's popularity gives the community a lot of helpful forums which practically help all the developers solve their nightmares.

Basics
PHP is really simple to develop and has a really short learning curve. The thing that you have to keep in mind is that PHP and HTML have to be integrated in order to show the the work done by a php script in the browser. So in a php file, you would have the relevant php script and if you would like to output some html elements in the browser, one would also have the equivalent html elements. In order to tell the server that a PHP script needs to be executed, the <?php & ?> tags need to be used.

Syntax & Variables

All the things that are going to be mentioned in this post can be looked out for in more detail on PHP's documentation which can be found on : http://www.php.net/manual/. Here is some of the syntax outlined.

Variables
Like any other language, PHP has different ways that one can use to create variables including Global, Normal variables & static variables. Here are examples of these mentioned methods.

Global: DEFINE("name","value");
Normal Variable: $var_name = "Hello World!";
static variable: static $static_var = 1234;

As you can see from the code above, PHP does not define any variable types and a variable can change it's type multiple types throughout its lifetime. There are limited types a variables can hold though and these are:

  • Boolean
  • Integers
  • Floating Point Numbers
  • Strings
  • Arrays
  • Objects
  • Resources
  • null

Array Declaration

//Simple Declaration
$array = array();


//Declare & Fill
$array = array("one"=>1,"two"=>2);


/*
Push into array - iterates the current index and gives the new index to this new variable */

array_push($array,"value");

//Insert a value into an array with a custom key
$array['key'] = "value";

IF statements & loops
Like any other language, PHP has different ways one can create variables including Global, Normal variables & static variables. Here are examples of these mentioned methods.

//IF statement

if($boolean == true){ 

//do something
}


/*
While loop - loops until $boolean remains true- breaks when $boolean becomes false
*/


while($boolean == true)
{
//do something
}


//FOR loop - loops until $i becomes equal to 10


for($i=0;$i<=10;$i++;){
//do something
}


/*
FOREACH loop - loops until $array values are all iterated and in foreach statement separates the array into a key and value pair.
*/


foreach($array as $key=>$value)
{
//do something
}

Printing Functions
There are various printing functions one can use in his application, here is an outline of what they are and what they do.

echo "hello world"; /* Outputs "hello world" - the most widely used to print something simple on a webpage */

print "Hello World"; /* Outputs "Hello World" - same as echo but works a little bit slower */

print_r(array("one","two")); /* Outputs "Array ( [0] => one [1] => two )" - mostly used for debugging purposes and shows the structure of a variable if it is an array or object. Otherwise it would print as the previous two functions do. */


var_dump("Hello World"); /* Outputs "string(11) "Hello World" " - Same as above, this is used for debugging purposes, the difference from the print_r() function is that this function also prints the datatypes of the printed variables. */

String functions and syntax
Here are a couple of things you can do with strings in PHP.
String Concatenation: "Hello " . "World" /* a dot(.) is the concatenation operator in PHP. */

Substring: substr($string,$start,$length);

String length: strlen($string);

String search: strpos($string,$search);

String Replace: str_replace($search,$replace_with,$string);

String Trim: trim($string);




Lab Session

1. Verifying that the local web server is working through a browser:
Steps: Open a browser and enter: http://localhost/ in the url bar, if it responds it works but if yes it will probably automatically redirect you to http://localhost/xampp.





2. Show an assosiative array of usernames and passwords in a table:
Code:
<?php


$users = array(
"user1"=>"password1",
"user2"=>"password2",
"user3"=>"password3",
"user4"=>"password4",
"user5"=>"password5",
);

?>
<table>
<tr><th>Username</th><th>Password</th></tr>
<?php foreach($users as $user => $password){?>
<tr><td><?php echo $user;?></td><td><?php echo $password;?></td></tr>
<?php }?>
</table>

3. Difference between echo and print:
The main difference between the echo & print function is mainly that print works a little bit slower than the echo function. The mostly used is echo but it comes down to only a matter of preference.


0 comments:

Post a Comment