Php: Difference between revisions

From XPUB & Lens-Based wiki
Line 60: Line 60:


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


== Function syntax ==
== Function syntax ==

Revision as of 09:03, 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!";
}

?>