PHP: What You Need To Know To Play With The Web

Advertisement

In this article, I’ll introduce you to the fundamentals of PHP. We’ll focus on using PHP to access Web services and on turning static HTML pages into dynamic ones by retrieving data from the Web and by showing different content depending on what the user has entered in a form or requested in the URL.

You won’t come out a professional PHP developer, but you’ll be well on your way to building a small page that uses Web services. You can find a lot of great PHP info on the Web, and most of the time you will end up on PHP.net1 itself. But I was asked repeatedly on several hack days and competitions to write this quick introduction article, so here it is.

What Is PHP?

PHP is a server-side language that has become a massive success for three reasons:

  • It is a very easy and forgiving language. Variables can be anything, and you can create them anytime you want.
  • It is part of the free LAMP stack (Linux, Apache, MySQL, PHP) and thus available on almost any server you can rent on the Web.
  • It does not need a special editor, environment or build process. All you do is create a file of the .php file type, mix PHP and HTML and then put it on your server for rendering.

Installing PHP Locally, And Your First Code

To run PHP locally on your computer, you’ll need a local server with PHP enabled. The easiest way to do this is to download and install MAMP for OS X2 or XAMPP for Windows3. Once you’ve installed any of these packages, you can start using PHP. Simply create a file named index.php in the htdocs folder of your MAMP or XAMPP installation.

In this file, type (or copy and paste) the following:

<?php
  $myname = 'Chris';
  echo '<p>This is PHP</p>';
  echo "<p>My name is $myname</p>"
  echo '<p>My name in another notation is still  '.$myname.'</p>';
?>

If you open this file in a browser by accessing your XAMPP or MAMP installation (via http://localhost/index.php or http://localhost:8888/index.php), you should see the following:

This is PHP
My name is Chris
My name in another notation is still Chris

But you won’t see that. The problem is that the third line does not end in a semicolon (;). This is an error. Depending on your PHP installation, you’ll get either an error message or simply nothing. If you get nothing, then find the file named php_error.log on your hard drive, and open it. It will tell you what went wrong.

The PHP error log as shown on a Mac

The first thing to remember, then, is that every line of PHP has to end in a semicolon. If we fix this problem, we get this result4:

PHP rendered in a browser

<?php
  $myname = 'Chris';
  echo '<p>This is PHP</p>';
  echo "<p>My name is $myname</p>";
  echo '<p>My name in another notation is still  '.$myname.'</p>';
?>

We can see here the first few important features of PHP:

  • PHP blocks start with <?php and end with ?>. Anything between these two commands is interpreted as being PHP and returned to the document as HTML.
  • Every line of PHP has to end with a semicolon (;), or else it is an error.
  • Variables in PHP start with a $, not with the var keyword as you do in JavaScript (this is where it gets confusing with jQuery and Prototype).
  • You print content to the document in PHP with the echo command. There is also a print command, which does almost the same, so you can use that, too.
  • In this example, we have defined a string named myname as “Chris”. To print it with the echo command surrounded by other text, you need to either embed the variable name in a text with quotation marks or concatenate the string with a full stop when you use single quotation marks. This is line 3 and 4: they do the same thing but demonstrate the different syntax. Concatenation is always achieved with a full stop, never with a + as you do in JavaScript.

You can jump in and out of PHP anywhere in the document. Thus, interspersing PHP with HTML blocks is totally fine. For example:

<?php
  $origin = 'Outer Space';
  $planet = 'Earth';
  $plan = 9;
  $sceneryType = "awful";
?>
<h1>Synopsis</h1>

<p>It was a peaceful time on planet <?php echo $planet;?> 
and people in the <?php echo $sceneryType;?> scenery were unaware 
of the diabolical plan <?php echo $plan;?> from <?php echo $origin;?> 
that was about to take their senses to the edge of what could be endured.</p>

This outputs the following5:

Rendered variables in PHP

Are you with me so far? To show something on the screen, particularly numbers or a string, we use echo. To show more complex structures, we need loops or specialized debugging methods.

Displaying More Complex Data Types

You can define arrays in PHP using the array() method:

$lampstack = array('Linux','Apache','MySQL','PHP');

If you simply want to display a complex data type like this in PHP for debugging, you can use the print_r() command:

$lampstack = array('Linux','Apache','MySQL','PHP');
print_r($lampstack);

This gives you all the information6, but it doesn’t help you structure it as HTML:

Displaying arrays with print_r

For this, you need to access the elements with the array counter. In PHP this is done with the [] brackets:

<ul>
<?php
$lampstack = array('Linux','Apache','MySQL','PHP');
echo '<li>Operating System:'.$lampstack[0] . '</li>';
echo '<li>Server:' . $lampstack[1] . '</li>';
echo '<li>Database:' . $lampstack[2] . '</li>';
echo '<li>Language:' . $lampstack[3] . '</li>';
?>
</ul>

See this demo in action7.

This is, of course, stupid programming because it is not flexible. If a computer is able to the dirty work for you, make it do it. In this case, we can define two arrays and use a loop8:

<ul>
<?php
$lampstack = array('Linux','Apache','MySQL','PHP');
$labels = array('Operating System','Server','Database','Language');
$length = sizeof($lampstack);
for( $i = 0;$i < $length;$i++ ){
  echo '<li>' . $labels[$i] . ':' . $lampstack[$i] . '</li>';
}
?>
</ul>

The for loop works the same as it does in JavaScript. The only difference is that you read the size of an array not with array.length but with sizeof($array).

Again, this example is not very clever because it assumes that both the $lampstack and the $labels array are of the same length and in the same order. Instead of using this, I’d use an associated array9:

<ul>
<?php
$lampstack = array(
  'Operating System' => 'Linux',
  'Server' => 'Apache',
  'Database' => 'MySQL',
  'Language' => 'PHP'
);
$length = sizeof($lampstack);
$keys = array_keys($lampstack);
for( $i = 0;$i < $length;$i++ ){
  echo '<li>' . $keys[$i] . ':' . $lampstack[$keys[$i]] . '</li>';
}
?>
</ul>

The function array_keys() gives you back all the keys of an array as an array itself. This way, we can display the keys and the values at the same time.

A shorter way to achieve the same principle, and which works with both arrays and objects, is to use the foreach() loop construct10:

<ul>
<?php
$lampstack = array(
  'Operating System' => 'Linux',
  'Server' => 'Apache',
  'Database' => 'MySQL',
  'Language' => 'PHP'
);
foreach( $lampstack as $key => $stackelm ){
  echo '<li>' . $key . ':' . $stackelm . '</li>';
}
?>
</ul>

This is the shortest way to display a complex construct. But it will fail when $lampstack is not an array. So, checking for sizeof() is still a good plan. You can do this with a conditional.

Using Conditionals

Conditionals are “if” statements, both in the English language and in almost any programming language I know. So, to test whether an array is safe to loop over, we could use the sizeof() test11:

<ul>
<?php
$lampstack = array(
  'Operating System' => 'Linux',
  'Server' => 'Apache',
  'Database' => 'MySQL',
  'Language' => 'PHP'
);
if( sizeof($lampstack) > 0 ){
  foreach( $lampstack as $key => $stackelm ){
    echo '<li>' . $key . ':' . $stackelm . '</li>';
  }
}
?>
</ul>

Common conditionals are:

  • if($x > 10 and $x < 20)
    Is $x bigger than 10 and less than 20?
  • if(isset($name))
    Has the variable $name been defined?
  • if($name == 'Chris')
    Does the variable $name have the value of "Chris"?
  • if($name == 'Chris' or $name == 'Vitaly')
    Does the variable $name have the value of "Chris" or "Vitaly"?

Cool, but what if we want to make this reusable?

Functions In PHP

To make a task even more generic, we can write a function. In this case, we put the loop and the testing in a function and simply call it with different arrays12:

<?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);

$awfulacting = array(
  'Natalie Portman' => 'Star Wars',
  'Arnold Schwarzenegger' => 'Batman and Robin',
  'Keanu Reaves' => '*'
);
renderList($awfulacting);
?>

Note that functions do not begin with a dollar sign.

We've already seen most of the magic of PHP. The rest is all about building functions to do all kinds of things: converting strings, sorting arrays, finding things in other things, accessing the file system, setting cookies and much more, each of which does one and only one thing right. I keep catching myself writing complex functions in PHP, only to realize from looking at the documentation that a native function already exists for it.

Interacting With The Web: URL Parameters

Let's start playing with the Web in PHP… or, more precisely, playing with information that comes from the browser's address bar or with forms that we can re-use. To get parameters from the current URL, we use the global $_GET array. So, if you call the index.php script with http://localhost/index.php?language=fr&font=large, you can change the display and locale by checking for these settings. The language parameter will be available as $_GET['language'], and the font parameter as $_GET['font']:

<?php
$name = 'Chris';

// if there is no language defined, switch to English
if( !isset($_GET['language']) ){
  $welcome = 'Oh, hello there, ';
}
if( $_GET['language'] == 'fr' ){
  $welcome = 'Salut, ';
}
switch($_GET['font']){
  case 'small':
    $size = 80;
  break;
  case 'medium':
    $size = 100;
  break;
  case 'large':
    $size = 120;
  break;
  default:
    $size = 100;
  break;
}
echo '<style>body{font-size:' . $size . '%;}</style>';
echo '<h1>'.$welcome.$name.'</h1>';
?>

This means we can now send URL parameters to change the behavior of this document:

changing the content and look of a document with parameters

Notice that predefining a set of values that are acceptable for a certain parameter is always best. In the earlier example, we may as well have set the font size in pixels as a parameter and then written that to the document—but then we would have needed a good validation script to prevent end users from sending bad values or even malicious code through the parameter.

Sending malicious code via a parameter without filtering is called cross-site scripting16 (XSS), and it is one of the big security problems of the Web. You can prevent it by not printing out the values of parameters, and instead using them in comparisons, and by using the filters provided by PHP17.

Say you want to allow users to enter data in a form that you will display later on. Make sure to filter out the results:

<?php
  $search_html = filter_input(INPUT_GET, 's',
                              FILTER_SANITIZE_SPECIAL_CHARS);
  $search_url = filter_input(INPUT_GET, 's',
                             FILTER_SANITIZE_ENCODED);
?>
<form action="index.php" method="get">
  <div>
    <label for="search">Search:</label>
    <input type="text" name="s" id="search" 
           value="<?php echo $search_html;?>">
  </div>
  <div class="bar"><input type="submit" value="Make it so"></div>
</form>
<?php
if(isset($_GET['s'])){
  echo '<h2>You searched for '.$search_html.'</h2>';
  echo '<p><a href="index.php?search='.$search_url.'">Search again.</a></p>';
}
?>

See this filtering example in action18. Without the filters, attackers could send parameters like index.php?s="<script>, which would execute third-party code on your website. With filtering, this malicious code is converted to HTML entities.

If you want to use POST as the method to send the data in your form, then the PHP variables will change accordingly to $_POST for the array and INPUT_POST for the filter.

Loading Content From The Web

PHP comes with a lot of file functions19 that allow you to read and write files from the hard drive or to load content from the Web. I've found, however, that for security reasons a lot of hosting companies disable them, especially when you try to read content from a third-party resource. The workaround is to use cURL to load information from the Web. cURL is a tool that allows you to make HTTP requests to retrieve information—a kind of browser in command-line form. I've written a detailed post about cURL and how to use it20. Here, then, is a simple use case to illustrate:

<?php
  // define the URL to load
  $url = 'http://www.smashingmagazine.com';
  // start cURL
  $ch = curl_init(); 
  // tell cURL what the URL is
  curl_setopt($ch, CURLOPT_URL, $url); 
  // tell cURL that you want the data back from that URL
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  // run cURL
  $output = curl_exec($ch); 
  // end the cURL call (this also cleans up memory so it is 
  // important)
  curl_close($ch);
  // display the output
  echo $output;
?>

If you run this in the browser21, you'll see Smashing Magazine's home page.

Smashing Magazine homepage loaded with cURL

You could also strip out content from the data:

<?php
  // define the URL to load
  $url = 'http://www.smashingmagazine.com';
  // start cURL
  $ch = curl_init(); 
  // tell cURL what the URL is
  curl_setopt($ch, CURLOPT_URL, $url); 
  // tell cURL that you want the data back from that URL
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  // run cURL
  $output = curl_exec($ch); 
  // end the cURL call (this also cleans up memory so it is 
  // important)
  curl_close($ch);
  // if a filter parameter with the value links was sent
  if($_GET['filter'] == 'links'){
    // get all the links from the document and show them
    echo '<ul>';
    preg_match_all('/<a[^>]+>[^</a>]+</a>/msi',$output,$links);
    foreach($links[0] as $l){
      echo '<li>' . $l . '</li>';      
    }
    echo'</ul>';
  // otherwise just show the page
  } else {
    echo $output;
  }
?>

If you open this in your browser, you'll get all of the links from Smashing Magazine22 and no other content.

Smashing magazine links

Nowadays, though, we are more likely to use APIs than to load websites this way, which is why we need a way to convert the XML and JSON that are returned from Web services into PHP-friendly data.

Displaying XML Content

The easiest way to deal with XML content in PHP is to use the SimpleXML functions of PHP23. Using these, we can turn a bunch of XML into a PHP object and loop over it. To show Smashing Magazine's RSS feed24, we can do the following:

<?php
  $url = 'http://rss1.smashingmagazine.com/feed/';
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $output = curl_exec($ch); 
  curl_close($ch);
  $data = simplexml_load_string($output);
  echo '<ul>';
  foreach($data->entry as $e){
    echo '<li><a href="' . $e->link[0]['href'] . 
         '">'.$e->title.'</a></li>';
  }
  echo '</ul>';
?>

The simplexml_load_string() function turns the XML document into a PHP object with arrays. How did I figure out to loop over data->entry and get the href via link[0]['href']? Simple. I did a print_r($output) and checked the source of the document by hitting Cmd + U in Firefox on my Mac. That showed me that this entry is an array. I then did a print_r($e) in the loop to see all the properties of every entry. If it is part of the @attributes array, then you need to use the [] notation.

That's all. The only stumbling block you will encounter is CDATA blocks and namespaces in SimpleXML. Stuart Herbert has a good introduction to these two issues in this article25.

Displaying JSON Content

The data format JSON26 is the low-fat alternative to XML. It is far less complex (e.g. no namespaces), and if you work in a JavaScript environment, it is native to the browser. This makes it very fast and easy to use, and for this reason it has started to become a popular data format for APIs. In essence, JSON is a JavaScript object. For example, I could write the LAMP stack example as follows:

{"lampstack":
  {
    "operatingsystem" : "Linux",
    "server" : "Apache",
    "database" : "MySQL",
    "language" : "PHP"
  }
}

You can convert this to PHP27 using the json_decode() method, and get it back as a PHP object:

<?php
  $json = '{
  "lampstack":
  {
    "operatingsystem":"Linux",
    "server":"Apache",
    "database":"MySQL",
    "language":"PHP"
    }
  }';
  print_r(json_decode($json));
?>

One API that returns JSON is the Twitter trends API28. If you load the API's URL with cURL and do a print_r() after json_decode(), you get the following back29:

<?php
  $url = 'http://search.twitter.com/trends.json';
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $output = curl_exec($ch); 
  curl_close($ch);
  $data = json_decode($output);
  print_r($data);
?>
stdClass Object
(
[trends] => Array
  (
    [0] => stdClass Object
    (
      [name] => #nowplaying
      [url] => http://search.twitter.com/search?q=%23nowplaying
    )

    [1] => stdClass Object
    (
      [name] => #Didntwannatellyou
      [url] => http://search.twitter.com/search?q=%23Didntwannatellyou
    )

    [2] => stdClass Object
    (
      [name] => #HappyBirthdayGagaBR
      [url] => http://search.twitter.com/search?q=%23HappyBirthdayGagaBR
    )

    [3] => stdClass Object
    (
      [name] => Justin Bieber
      [url] => http://search.twitter.com/search?q=%22Justin+Bieber%22
    )

    [4] => stdClass Object
    (
      [name] => #FreakyFactSays
      [url] => http://search.twitter.com/search?q=%23FreakyFactSays
    )

    [5] => stdClass Object
    (
      [name] => #YouSoGangsta
      [url] => http://search.twitter.com/search?q=%23YouSoGangsta
    )

    [6] => stdClass Object
    (
      [name] => I ?
      [url] => http://search.twitter.com/search?q=%22I+%E2%99%A5%22
    )

    [7] => stdClass Object
    (
      [name] => #MeMyselfandTime
      [url] => http://search.twitter.com/search?q=%23MeMyselfandTime
    )

    [8] => stdClass Object
    (
      [name] => #2010yearofJonas
      [url] => http://search.twitter.com/search?q=%232010yearofJonas
    )

    [9] => stdClass Object
    (
      [name] => Easter
      [url] => http://search.twitter.com/search?q=Easter
    )
  )
  [as_of] => Sun, 28 Mar 2010 19:31:30 +0000
)

You can then use a simple loop to render the current trends as an unordered list30:

<?php
  $url = 'http://search.twitter.com/trends.json';
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $output = curl_exec($ch); 
  curl_close($ch);
  $data = json_decode($output);
  echo '<h2>Twitter trending topics ('.$data->as_of.')</h2>';
  echo '<ul>';
  foreach ($data->trends as $t){
    echo '<li><a href="'.$t->url.'">'.$t->name.'</a></li>';
  }
  echo '</ul>';
?>

Putting It All Together

Let's do a quick example using all of the things we've learned so far: a simple search interface for the Web.

Using Yahoo's YQL31, it is pretty easy to do a Web search for "cat" with the command select * from search.web where query="cat" sent to the YQL endpoint. You can define JSON as the return format, and the rest means you simply enhance the earlier form example32:

<?php
  $search_html = filter_input(INPUT_GET, 's', FILTER_SANITIZE_SPECIAL_CHARS);
  $search_url = filter_input(INPUT_GET, 's', FILTER_SANITIZE_ENCODED);
?>
<form action="index.php" method="get">
  <div>
    <label for="search">Search:</label>
    <input type="text" name="s" id="search" 
           value="<?php echo $search_html;?>">
   <input type="hidden" name="demo" value="17">
   <input type="submit" value="Make it so">
  </div>
</form>
<?php
if(isset($_GET['s'])){
  echo '<h2>You searched for '.$search_html.'</h2>';
  $yql = 'select * from search.web where query="'.$search_url.'"';
  $url = 'http://query.yahooapis.com/v1/public/yql?q='.
          urlencode($yql).'&format=json&diagnostics=false';
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $output = curl_exec($ch); 
  curl_close($ch);
  $data = json_decode($output);
  echo '<ul>';
  foreach ($data->query->results->result as $r){
    echo '<li><h3><a href="'.$r->clickurl.'">'.$r->title.'</a></h3>'.
         '<p>'.$r->abstract.' <span>('.$r->dispurl.')</span></p></li>';
  }
  echo '</ul>';

  echo '<p><a href="index.php?search='.$search_url.'&demo=17">Search again.</a></p>';
}
?>

Interaction With JavaScript

One thing people keep asking about is how to send information from PHP to JavaScript and back. This can be done in a few ways.

  • To send information from JavaScript to PHP, you need to either alter the href of a link or populate a hidden form field. The other solution of course is to use AJAX.
  • To send information from PHP to JavaScript, simply render a script element and write out the PHP information with an echo statement.
  • Using PHP's header() function and json_encode(), you can send data back to the browser as JavaScript, which allows us to use it as a src attribute of a script node.

For example, to get Smashing Magazine's RSS feed as a JavaScript object, you could do the following33:

<?php
  header('Content-type: text/javascript');
  $url = 'http://rss1.smashingmagazine.com/feed/';
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $output = curl_exec($ch); 
  curl_close($ch);
  $data = simplexml_load_string($output);
  $data = json_encode($data);
  echo 'var smashingrss='.$data;
?>

You could then use this in a JavaScript block:

<script src="http://icant.co.uk/articles/phpforhacks/index.php?demo=18"></script>
<script>alert(smashingrss.title);</script>

Using header() and json_encode(), you could do any complex conversion and filtering in PHP and re-use it in JavaScript.

Summary

I hope this gives you an idea of what PHP is and how you can use it to access Web services and to build your own APIs to re-use in JavaScript. Using PHP for the Web boils down to a few tricks:

  • Use cURL to load data from Web resources;
  • Convert information with simplexml_load_string() and json_decode();
  • Check the structure of returned information with print_r();
  • Loop over information with foreach();
  • Use the $_GET[] and $_POST[] arrays to re-use form data and URL parameters;
  • Filter information from the user and from URLs using the built-in PHP filtering methods.

A lot of documentation is out there, and your best bet is to go directly to the PHP home page34 to read or download the documentation. Check out the user comments in particular, because that is where the real gems and examples of implementation appear.

(al)

Footnotes

  1. 1 http://php.net
  2. 2 http://www.mamp.info/en/index.html
  3. 3 http://www.apachefriends.org/en/xampp-windows.html
  4. 4 http://icant.co.uk/articles/phpforhacks/index.php
  5. 5 http://icant.co.uk/articles/phpforhacks/index.php?demo=1
  6. 6 http://icant.co.uk/articles/phpforhacks/index.php?demo=2
  7. 7 http://icant.co.uk/articles/phpforhacks/index.php?demo=3
  8. 8 http://icant.co.uk/articles/phpforhacks/index.php?demo=4
  9. 9 http://icant.co.uk/articles/phpforhacks/index.php?demo=5
  10. 10 http://icant.co.uk/articles/phpforhacks/index.php?demo=6
  11. 11 http://icant.co.uk/articles/phpforhacks/index.php?demo=7
  12. 12 http://icant.co.uk/articles/phpforhacks/index.php?demo=8
  13. 13 http://icant.co.uk/articles/phpforhacks/index.php?demo=9
  14. 14 http://icant.co.uk/articles/phpforhacks/index.php?demo=9&language=fr
  15. 15 http://icant.co.uk/articles/phpforhacks/index.php?demo=9&language=fr&font=large
  16. 16 http://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29
  17. 17 http://uk.php.net/manual/en/intro.filter.php
  18. 18 http://icant.co.uk/articles/phpforhacks/index.php?demo=10
  19. 19 http://uk2.php.net/manual/en/ref.filesystem.php
  20. 20 http://www.wait-till-i.com/2009/12/18/curl-your-view-source-of-the-web/
  21. 21 http://icant.co.uk/articles/phpforhacks/index.php?demo=11
  22. 22 http://icant.co.uk/articles/phpforhacks/index.php?demo=12&filter=true
  23. 23 http://php.net/manual/en/book.simplexml.php
  24. 24 http://icant.co.uk/articles/phpforhacks/index.php?demo=13
  25. 25 http://blog.stuartherbert.com/php/2007/01/07/using-simplexml-to-parse-rss-feeds/
  26. 26 http://json.org
  27. 27 http://icant.co.uk/articles/phpforhacks/index.php?demo=14
  28. 28 http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-trends
  29. 29 http://icant.co.uk/articles/phpforhacks/index.php?demo=15
  30. 30 http://icant.co.uk/articles/phpforhacks/index.php?demo=16
  31. 31 http://developer.yahoo.com/yql/
  32. 32 http://icant.co.uk/articles/phpforhacks/index.php?demo=17
  33. 33 http://icant.co.uk/articles/phpforhacks/index.php?demo=18
  34. 34 http://php.net

↑ Back to top Tweet itShare on Facebook

An international Developer Evangelist working for Mozilla in the lovely town of London, England.

Advertisement
  1. 1

    Christian Sciberras

    April 19, 2010 5:29 am

    Was? Delphi still exists today (fact: Skype was made in Delphi).
    Also, for your information, there’s a free project like Delphi called Lazarus. It is also cross-platform (linux and even embedded devices such as for gumstix).

    As to the article, it does look great, however, if you want to style array elements, there’s a faster way:

    echo ”;
    print_r($array);
    echo ”;

    Or in short:

    echo ”.print_r($array,true).”;

    0
  2. 152

    Christian Sciberras

    April 19, 2010 5:32 am

    Yeah, how do I show a messagebox in jquery, eh?!!

    (I was just alerting you about my sarcasm)

    0
  3. 303

    “no wait, i had to teach my teacher turbo pascal” hehehe, same here.

    0
  4. 454

    Thanks for this nice intro in php!
    bookmarked!

    0
  5. 605

    Great points – however, crawling the DOM usually works (even on invalid HTML – I’ve never had it actually break) if you use the error suppression operator. All it’s suppressing (usually, in my experience) is a warning that the HTML doesn’t validate. It should still work. Not exactly ideal, but definitely advisable on production code.

    0
  6. 756

    Thumbs up for an excellent introduction to PHP. I’m now enjoying my 39th year as a developer, have used many languages, and found it informative. I’ve had little exposure to PHP and found the sections on displaying XML and JSON content interesting. Interesting enough that I’ll probably dive a little deeper.

    As an aside, since the article is an introduction it can’t be expected to teach web application security and database concepts. But “Big ups” for including sanitizing URL content.

    0
  7. 907

    Nice job. I thought this article was specific to the desired topic and the right length. Your use of simple examples that offer re-enforcment of the subject material was perfect.

    I often find interesting topics with no working examples, or examples that don’t produce a truly functional model – this article did not fall pray to either of these pitfalls.

    Thanks for the time and effort for an informative subject. :-)

    0
  8. 1058

    Yeah, people who do not read articles (paragraph #2) might come up with this conclusion.

    Actually after almost 14 years of development I can say that good development is designers and engineers working together. If you can’t accept that you will have a bad time.

    2
  9. 1209

    Its a gr8 tutorial..Thanks a lot mate..

    1
  10. 1360

    Great we’re going to have designers tell clients and others now.. ‘ Oh hey I know PHP ..’ an erroneous statement……

    This is why Smashing Magazine should just stick to posting about Design in general. First off, even the author of this article in the posts tells you this isn’t even an introduction to PHP itself. If designers want to get into development and think they can handle learning PHP… well you’re in way over your head. You have to understand that there are two ends to the web, front and back, and if you don’t already understand this ….. .. just stick to designing and I guess WordPress…

    Leave the real development to the Web Developers of the world and we’ll leave the designing to you the designers.

    If you’re a designer and you want to learn about development, I suggest you understand the full capacity of a front-end developer… ( A REAL ONE THAT DOESN’T WORK IN TRANSITIONAL DOCTYPES!!!! )

    This article proves.. that there is a design community and a developer community. You can not have a mixture of both.. it goes against the logic of the way the web is made to function anyway.

    -1
  11. 1511

    Never learned in school something like this. I’ve learned PHP a few years ago from a series of video tutorials (made in 1992 or so, by some guys.. great stuff..), and also practicing and practicing.
    I also learned javascript, CSS, and SQL…. all you need for web development.

    Great article, Thanks,
    Andrei, from Romania

    0
  12. 1662

    Great, now translate it to RoR :)

    0
  13. 1813

    Thank you, yes. I am one of them!

    1
  14. 1964

    What You Need To Know To Play With The Web

    0
  15. 2115

    We can see here the first few important features of PHP:

    0
  16. 2266

    Rails FTW!!

    -1
  17. 2417

    I’d love to see articles like this for helping people get started with other languages, especially those that seem more daunting like Java. Good stuff!

    0
  18. 2568

    For those of you who despised reading the article because of it’s basic-ness. Please be sure to start with the first sentence of an article in the future. Fundamentals of PHP doesn’t mean they’re going to show us how to build the next operating system…just give a start to those of us who haven’t gotten it. i also learned Turbo Pascal programming in school. Thanks again SM.

    0
  19. 2719

    Saying that PHP is a scripting language as opposed to a programming language is no more detracting from it than saying that a Porsche is a sports car, not a minivan. It is what it is. Nobody is bombing on XHTML for being a markup language, because when you need a markup language, that’s what you need. The same goes for scripting languages.

    1
  20. 2870

    I just wanted to say thanks for an informative article. Coming from a web design background but knowing C# quite extensively, your examples helped me jump in quickly. Also, I’d like to point out to the detractors … just look at the title, realize the intent of the article, and form an opinion based on that.

    0
  21. 3021

    Just amazing the number of responses this article has generated so far! One has to spend more time reading the comments than reading the article itself…
    Anyway, i think that Heilmann did a great job writing this article: i like the way he proceeds and for me it makes perfect sense. I am sure this is far from being complete and i will be surprised if any one-or-more-pages tutorial would claim to be a complete ref. It is just an initiation into PHP. For those who want to delve into more “specilaised” stuff, they can pick some books from any bookshop and here they go. I think it is always about the beginnings: I might read an advanced book and I will just immediately think: PHP is not for me and I will try something else instead. This article does the opposite: After reading this, I am sure many of us newbies will get a flavour of what it is without being led to the “forceful” conclusion that it is only for those “High IQs” out there. So, for me, reading this is really worth it and i will try to “polish” my skills gradually. i promise i will not pretend to be an expert until I read all the manuals i can find.. Bottom of the line: we know you know a lot of things but only “nice” articles like this one will entice us to learn more PHP.

    2
  22. 3172

    Amjad Iqbal Khan

    May 4, 2010 12:58 am

    I think this tutorial is also useful even for the experienced programmers. This kind of tutorials should be encoureged.

    0
  23. 3323

    thanks for your information,,,
    its very usefull

    0
  24. 3474

    Nice article, nice information…

    0
  25. 3625

    Completely agree. There aren’t enough websites out there that stress the importance of security whilst explaining the basics of PHP through tutorials. I’m not saying that they should jump in with the paranoia from step 1 explaining about encryption / decryption, SSL, security, not using globals etc – but it would be nice to state it somewhere in the article.

    I know this from personal experience. I built a fairly big sports website averaging 2,500 hits a month for the past 3 years, and only after 18 months of doing tutorials and constantly improving my own code did somebody point out that a massive flaw that I hadn’t come across. Using global variables.

    Only because that was the way the server was setup and none of the tutorials I’d read had ever covered security – I just assumed it would all be taken care of. Now though I’ve done a full 180, and I’m on constant lookout for any security flaws in my code. This really is an issue of massive importance and should be highlighted more.

    0
  26. 3776

    Hello,

    nice article, see how to submit a form using curl http://newdailyblog.blogspot.com/2010/06/submitting-form-remotely-using-curl.html.

    0
  27. 3927

    Great Tutorial. .

    1
  28. 4078

    Stuck at the cURL functions. Any reason why this wouldn’t do anything locally?
    I have everything installed locally (recent download) php5, apache2, etc. on ubuntu 10.04.
    Can’t find a reference to the curl extension in php.ini?
    To be exact PHP Version 5.3.2-1ubuntu4.2

    Note: Same code works uploaded to a webhost.

    -2
  29. 4229

    Why this form in not working
    <?php
    //build mail
    $to="simon@snowflakecreative.co.uk";
    //$to="thapa.nitesh@kreativefingers.com";
    $subject = "Ace Poker Massage";
    // print_r($_POST);
    //exit;
    $Name=$_REQUEST['name'];
    $EmailAddress=$_REQUEST['email'];
    $Comments=$_REQUEST['comments'];
    $url ='';

    $msg= '

    -1
  30. 4380

    ‘;

    //echo $msg;
    // exit;
    //send mail
    $headers = “MIME-Version: 1.0n”;
    $headers .= “Content-type: text/html; charset=iso-8859-1n”;
    $headers .= “X-Priority: 3n”;
    $headers .= “X-MSMail-Priority: Normaln”;
    $headers .= “X-Mailer: phpn”;
    $headers .= “From: “”. $to .”” n”;

    mail($to, stripslashes($subject), stripslashes($msg), $headers) or die(“Could not send e-mail – Error A46GY7″);
    //send the user to the response page

    $msgmain=’

    ‘;
    $subjectmain=”Forward Email From Admin of Ace Poker Massage”;
    //send mail
    $headersmain = “MIME-Version: 1.0n”;
    $headersmain .= “Content-type: text/html; charset=iso-8859-1n”;
    $headersmain .= “X-Priority: 3n”;
    $headersmain .= “X-MSMail-Priority: Normaln”;
    $headersmain .= “X-Mailer: phpn”;
    $headersmain .= “From: “”. $to .”” n”;

    mail($Email, stripslashes($subjectmain), stripslashes($msgmain), $headersmain) or die(“Could not send e-mail – Error A46GY7″);
    $redirect = “Location:../thanku.html”;
    header($redirect);

    ?>

    0
  31. 4531

    cURL issues again, this time with the XML reader.

    1
  32. 4682

    Terrible example
    <?php
    $url = 'http://rss1.smashingmagazine.com/feed/&#039;;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $data = simplexml_load_string($output);
    echo '';
    foreach($data->entry as $e){
    echo ‘link[0][‘href’] .
    ‘”>’.$e->title.’
    ‘;
    }
    echo ”;
    ?>
    Curl does nothing.
    In the foreach loop, what is $data->entry as $e? Specifically *entry*
    What is $e->link[0][‘href’] . ? Presumably something to do with the XML file element?
    What is title in ‘”>’.$e->title.’‘; Again I something to do with the XML file?
    In fact, please explain what -> does.
    Tried doing print_f($e); inside the loop but nothing happens.
    Gonna have a look on php.net for some better examples.

    -1
  33. 4833

    This has been very helpful. You have some great sets of code here. Thanks for sharing.

    1
  34. 4984

    You don’t learn shit in HS about computers. The teachers don’t know what they’re talking about and need students to help research and find out the info themselves to study and pass exams.

    0
  35. 5135

    I was just browsing through Smashing a bit and found this one, which I think is great. I am a passionate developer and know PHP quite well (I hope) but there were a few “a-ha’s” for me in this one as well and I think it is very clean and nicely written, so beginners: start here ;)

    0
  36. 5286

    David Higginbotham

    February 3, 2012 6:29 pm

    Awesome article! I was able to send this to a client and have him grasp some basic functionality. (Rare, I know — Excellent article)

    Cheers

    0
  37. 5437

    Are you referring to the fact that the link to all the links from Smashing Magazine doesn’t produce the desired result? The reason is that the link is coded wrong in the article. Until it gets fixed, simply copy the link, paste it into your browser, then remove the word ‘true’ at the end and replace it with the word ‘links’ (without the single quotes.)

    Jim

    0

↑ Back to top