Glory with PHP Anshu Prateek
PHP HyperText Processor
Agenda • The basics • Enough PHP to handle simple request • How to talk to backend data store using PHP • How to parse/generate XML/JSON in PHP
What is it..?! • What is PHP?! • RasmusLerdorf – 1995 • Compile or interpret? • CLI?
Pre-requisites LAMP/WAMP/MAMP L/W/M – Linux/Windows/Mac A – Apache M – Mysql P - PHP
About me.. Err.. PHP Latest Version : 5.4.0 (released on march 01, 2012) Presently hosted on github: https://github.com/php/php-src Written in C, you can write your own modules!
The basics • Starts with <?php • <? Also accepted but recommended not to use due to xml usage • Closing ?> is not mandatory unless code structure requires closure. • Infact if possible, do not use closing ?> , helps avoid “header already sent” errors. • Double quotes allow variable substitution, single quotes don‟t. • Variables start with $
Helooo world! • <?php • php -r '$greet = • $greet = “hello”; "hello";echo "$greet • Echo $greet.‟world‟; world";'
Ways to put long text • <?php • $variable = „the long long long copied wikipedia text”; ?> <html> … <body> <?php echo $text; ?> </body>
Or use HEREDOC • <?php • $name = „doctor who‟; • $html=<<<HTML • this is some html • And some more html • And only html! • And I can use $name „s alien powers as well! • HTML; • echo $html;
Strings • http://us2.php.net/manual/en/ref.strings.php • The various inputs and outputs need to be formatted. Various functions. string substr ( string $string , int $start [, int $length ] ) • string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) • And many more…
Arrays • $array = array( "foo" => "bar", 42 => 24, "multi" => array( "dimensional" => array( "array" => "foo" ) ) ); • var_dump($array["foo"]); var_dump($array[42]); var_dump($array["multi"]["dimensional"]["array "]);
Arrays.. • http://www.php.net/manual/en/function.array.ph p
Functions • Function <?php function renderList($array){ if( sizeof($array) > 0 ){ echo '<ul>'; foreach( $array as $key => $item ){ echo '<li>' . $key . ':' . $item . '</li>'; } echo '</ul>'; } } $lampstack = array( 'Operating System' => 'Linux', 'Server' => 'Apache', 'Database' => 'MySQL', 'Language' => 'PHP' ); renderList($lampstack); ?>
Talking with your page • $_GET • Example.com/?query=hacku • $query = $_GET[„query‟] • $_POST • <form id=“iitm> • <input type=“text” name=“userid” /> • <submit> • </form> $id = $_POST[„userid‟]; $_COOKIES;
Fetching data from the web.. • BOSS • Uses OAUTH
Adding it all up together.. Pictionary! 1)Getting the user provided text. 2) Remove the blacklist words. 3) Get image for the rest of the words. Show me the code..! https://github.com/anshprat/hacku

PHP Basics and Demo HackU

  • 1.
    Glory with PHP Anshu Prateek
  • 2.
  • 3.
    Agenda • The basics • Enough PHP to handle simple request • How to talk to backend data store using PHP • How to parse/generate XML/JSON in PHP
  • 4.
    What is it..?! • What is PHP?! • RasmusLerdorf – 1995 • Compile or interpret? • CLI?
  • 5.
    Pre-requisites LAMP/WAMP/MAMP L/W/M – Linux/Windows/Mac A – Apache M – Mysql P - PHP
  • 6.
    About me.. Err..PHP Latest Version : 5.4.0 (released on march 01, 2012) Presently hosted on github: https://github.com/php/php-src Written in C, you can write your own modules!
  • 7.
    The basics • Startswith <?php • <? Also accepted but recommended not to use due to xml usage • Closing ?> is not mandatory unless code structure requires closure. • Infact if possible, do not use closing ?> , helps avoid “header already sent” errors. • Double quotes allow variable substitution, single quotes don‟t. • Variables start with $
  • 8.
    Helooo world! • <?php • php -r '$greet = • $greet = “hello”; "hello";echo "$greet • Echo $greet.‟world‟; world";'
  • 9.
    Ways to putlong text • <?php • $variable = „the long long long copied wikipedia text”; ?> <html> … <body> <?php echo $text; ?> </body>
  • 10.
    Or use HEREDOC • <?php • $name = „doctor who‟; • $html=<<<HTML • this is some html • And some more html • And only html! • And I can use $name „s alien powers as well! • HTML; • echo $html;
  • 11.
    Strings • http://us2.php.net/manual/en/ref.strings.php • Thevarious inputs and outputs need to be formatted. Various functions. string substr ( string $string , int $start [, int $length ] ) • string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) • And many more…
  • 12.
    Arrays • $array =array( "foo" => "bar", 42 => 24, "multi" => array( "dimensional" => array( "array" => "foo" ) ) ); • var_dump($array["foo"]); var_dump($array[42]); var_dump($array["multi"]["dimensional"]["array "]);
  • 13.
  • 14.
    Functions • Function <?php function renderList($array){ if( sizeof($array) > 0 ){ echo '<ul>'; foreach( $array as $key => $item ){ echo '<li>' . $key . ':' . $item . '</li>'; } echo '</ul>'; } } $lampstack = array( 'Operating System' => 'Linux', 'Server' => 'Apache', 'Database' => 'MySQL', 'Language' => 'PHP' ); renderList($lampstack); ?>
  • 15.
    Talking with yourpage • $_GET • Example.com/?query=hacku • $query = $_GET[„query‟] • $_POST • <form id=“iitm> • <input type=“text” name=“userid” /> • <submit> • </form> $id = $_POST[„userid‟]; $_COOKIES;
  • 16.
    Fetching data fromthe web.. • BOSS • Uses OAUTH
  • 17.
    Adding it allup together.. Pictionary! 1)Getting the user provided text. 2) Remove the blacklist words. 3) Get image for the rest of the words. Show me the code..! https://github.com/anshprat/hacku

Editor's Notes

  • #5 What is PHP?!PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.Code is interpreted by a Web server with a PHP processor module which generates the resulting Web page.Includes a command-line interface capability and can be used in standalone graphical applications