Php: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(31 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<slidy theme="aa" />


==PHP==
* Server side programming language
* Server side programming language
* LAMP model
[[file:LAMP.png | 640px ]]


[[file:LAMP.jpg]]


* frameworks
==Hello World==
<source lang="php">
<!DOCTYPE html>
<meta charset=utf-8>
<title>PHP Test</title>
<?php
echo 'Hello World';
?>
</source>


* php.net
* Every block of PHP code must start with <?php and end with ?> (or <? and ?>)
* You need to place a semi-colon (;) at the end of each line of PHP code.
* variable names must start with a dollar sign ($).
* Try to build this in a index.php


* LAMP
==pros==
* widely supported (php.net)
* frameworks (Zend, CodeIgniter, CakePHP etc)
* easy to learn
* copy paste
* Object Oriented Programming
* Supports anything from database managing to image modification


* mixing
==cons==
 
* too easy -> not safe (SQL / BASH  injection)
* error logs /cat /var/log/apache2/error.log  / run file
* easy to write bad spagetti code
* messy, inconsistent language
* not so widely used for applications


<br>
* to view the error logs: 
<pre>
cat /var/log/apache2/error.log 
</pre>
* or run the file
<pre>
php index.php
</pre>


== Mixing with HTML==
<source lang="php">
<source lang="php">
<!DOCTYPE html>
<?php
<meta charset=utf-8>
// this is a PHP comment
<title>PHP Test</title>
$myHeader = "Header made with echo";
<?php
echo "<h3>".$myHeader."</h3>";
echo 'Hello World';
?>
?>
<!--  Is similar to (note HTML comment)  -->
<?php $myHeader2 = "Header made with with HTML"; ?>
<h3> <?php echo $myHeader2; ?> </h3>
</source>
</source>


* you can insert any amount of php blocks in a HTML page
* and switch between HTML and PHP


== Function syntax ==
<source lang="php">
<source lang="php">
function myFunction() { //declares a function, this is named myFunction
function myFunction() { //declares a function, this is named myFunction
Line 30: Line 68:
}
}


echo 'My name is ' . myFunction() . '!'; //outputs the text concatenated with the return value of myFunction.
echo 'My name is ' . myFunction() . '!';  
//myFunction is called as a result of this syntax.
 
//outputs the text concatenated with the return value of myFunction.
//myFunction is called as a result of this syntax.
//The result of the output will be 'My name is John Doe!'
//The result of the output will be 'My name is John Doe!'
</source>
* function FunctionName (Arguments){ echo 'test'; }
== Weirdness of PHP ==
<source lang="php">
<?php
if("foo" == TRUE){
echo 'YES! foo == TRUE, because it is? Well okay....';
}
if("foo" == 0){
echo 'Huh? "foo" is also a number?';
}
if(TRUE == 0){
echo 'therefor True is also 0, right?! ';
}else{
echo "WRONG!";
}
?>
</source>
=== A simple PHP portfolio page ===
<source lang='php'>
<html>
<head>
    <style>
    #menu{
        border:1px solid black;
        float:left;
        width:200px;
        padding:10px;
    }
    #content{
        border:1px solid black;
        float:left;
        width:600px;
        padding:10px;
    }
    #content img{width:100%;height:auto;}
    </style>
</head>
<body>
    <div id='menu'>
        <h2>Menu</h2>
        <?php
            echo '<h3>here we see the project names</h3>';
            $directoryName = 'content/';
            //check if it can open the directory
            if ($openDirectory = opendir($directoryName)) {
                // read each file/directory within
                while (($file = readdir($openDirectory)) !== false) {
                    // skip other paths
                    if ($file == '.' or $file == '..') continue;
                    // ?page= !!!!!!
                    // notice the dot as concat
                    echo "<a href='index.php?page=".$file."'>".$file."</a><br/>";
                }
                closedir($openDirectory);
            }
           
        ?>
    </div>
    <div id='content'>
      <?php
        // GET the php variable from the url!!
        // GET looks at our current URL, and retrieves variables
        // http://pzwart3.wdka.hro.nl/~tklok/portfolio/index.php?folder=burgers
        // $_GET['folder'] => burger
   
        $dir_path = $directoryName . $_GET["folder"].'/';
        // check if it can open the directory
        if ($dir_handler = opendir($dir_path)) {
            // read each file/directory within
            $counter=0;
            while (($file = readdir($dir_handler)) !== false) {
                // skip other paths
                if ($file == '.' or $file == '..') continue;
                // only JPGs for now so....
                // check on the extention
                //
                $extention = substr(strrchr($file,'.'),1);
                if ($extention =='jpg'){
                    echo '<img class="jpg" id="img'.$counter. '" src="'.$dir_path.$file.'" />';
                }
                if ($extention == 'txt'){
                    //echo $dir_path.$file;
                    $filetext= file_get_contents($dir_path.$file);
                    echo $filetext;
                }
            $counter+=1;
            }
        closedir($dir_handler);
        }
    ?>
    </div>
</body>
</html>
</source>
</source>

Latest revision as of 11:40, 22 April 2013

<slidy theme="aa" />

PHP

  • Server side programming language
  • LAMP model

LAMP.png


Hello World

<!DOCTYPE html>
<meta charset=utf-8>
<title>PHP Test</title>
<?php
 echo 'Hello World';
?>
  • Every block of PHP code must start with <?php and end with ?> (or <? and ?>)
  • You need to place a semi-colon (;) at the end of each line of PHP code.
  • variable names must start with a dollar sign ($).
  • Try to build this in a index.php

pros

  • widely supported (php.net)
  • frameworks (Zend, CodeIgniter, CakePHP etc)
  • easy to learn
  • copy paste
  • Object Oriented Programming
  • Supports anything from database managing to image modification

cons

  • too easy -> not safe (SQL / BASH injection)
  • easy to write bad spagetti code
  • messy, inconsistent language
  • not so widely used for applications


  • to view the error logs:
cat /var/log/apache2/error.log   
  • or run the file
php index.php

Mixing with HTML

<?php 
 // this is a PHP comment
$myHeader = "Header made with echo";
echo "<h3>".$myHeader."</h3>";
?>

<!--  Is similar to (note HTML comment)  -->
<?php $myHeader2 = "Header made with with HTML"; ?>
<h3> <?php echo $myHeader2; ?> </h3>
  • you can insert any amount of php blocks in a HTML page
  • and switch between HTML and PHP

Function syntax

function myFunction() { //declares a function, this is named myFunction
 return 'John Doe'; //returns the value 'John Doe'
}

echo 'My name is ' . myFunction() . '!'; 

//outputs the text concatenated with the return value of myFunction.
//myFunction is called as a result of this syntax.
//The result of the output will be 'My name is John Doe!'
  • function FunctionName (Arguments){ echo 'test'; }

Weirdness of PHP

<?php
if("foo" == TRUE){
	echo 'YES! foo == TRUE, because it is? Well okay....';
}

if("foo" == 0){
	echo 'Huh? "foo" is also a number?';
}

if(TRUE == 0){
	echo 'therefor True is also 0, right?! ';
}else{
	echo "WRONG!";
}

?>

A simple PHP portfolio page

<html>

<head>
    <style>
    #menu{
        border:1px solid black; 
        float:left; 
        width:200px;
        padding:10px;
    }
    #content{
        border:1px solid black; 
        float:left;
        width:600px;
        padding:10px;
    }
    #content img{width:100%;height:auto;}
    </style>
</head>

<body>

    <div id='menu'>
        <h2>Menu</h2>

        <?php
            echo '<h3>here we see the project names</h3>';
            $directoryName = 'content/';

            //check if it can open the directory
            if ($openDirectory = opendir($directoryName)) {
                // read each file/directory within
                while (($file = readdir($openDirectory)) !== false) {
                    // skip other paths
                    if ($file == '.' or $file == '..') continue;
                    // ?page= !!!!!! 
                    // notice the dot as concat
                    echo "<a href='index.php?page=".$file."'>".$file."</a><br/>";
                }
                closedir($openDirectory);
            }
            
        ?>
    </div>

    <div id='content'>
       <?php


        // GET the php variable from the url!!
        // GET looks at our current URL, and retrieves variables
        // http://pzwart3.wdka.hro.nl/~tklok/portfolio/index.php?folder=burgers
        // $_GET['folder'] => burger
     
        $dir_path = $directoryName . $_GET["folder"].'/';
        // check if it can open the directory

        if ($dir_handler = opendir($dir_path)) {
            // read each file/directory within
            $counter=0;
            while (($file = readdir($dir_handler)) !== false) {

                // skip other paths
                if ($file == '.' or $file == '..') continue;
                // only JPGs for now so....
                // check on the extention
                //
                $extention = substr(strrchr($file,'.'),1);
                if ($extention =='jpg'){
                    echo '<img class="jpg" id="img'.$counter. '" src="'.$dir_path.$file.'" />';
                }
                if ($extention == 'txt'){
                    //echo $dir_path.$file;
                    $filetext= file_get_contents($dir_path.$file);
                    echo $filetext;
                }
            $counter+=1;
            }
        closedir($dir_handler);
        }
    ?>
    </div>

</body>

</html>