Mystery behind a blank page..

As you start programming in PHP, you can sometimes hit a scenario where the PHP page you are developing appears as a BLANK page when you request the PHP page from a browser. So what should you do under such circumstances? Here are a few tips:

  • Check server logs - Check the error log for your apache server. You should probably get some hints from the error log. Check the apache access log to make sure you are requesting for the right assets.
  • If checking the server logs don’t help, try adding some print/echo statements in your code, to see where it is exactly failing. Be smart when you do this, don’t start adding print statements everywhere in the page. Spread your debug print statements over the page to start with and then start moving down to specific areas.

Hope that helps.

Basics on how to open, read, write and close files in PHP?

Lets say we would like to open a file and print one line from it.

To do this you first have to open a file using fopen funtion. The fopen() function opens a file or URL.

<?php
$file = fopen("test.txt","r");
$file = fopen("/home/test/test.txt","r");
$file = fopen("/home/test/test.gif","w+");
$file = fopen("http://www.example.com/","r");
$file = fopen("ftp://user:password@example.com/test.txt","w");
?>

the second argument in the function above is the mode in which you want to open the file. You can open the file only for reading, only for writing, for appending, for reading and writing etc as explained below:
Possible values:

  • “r” (Read only. Starts at the beginning of the file)
  • “r+” (Read/Write. Starts at the beginning of the file)
  • “w” (Write only. Opens and clears the contents of file; or creates a new file if it doesn’t exist)
  • “w+” (Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist)
  • “a” (Write only. Opens and writes to the end of the file or creates a new file if it doesn’t exist)
  • “a+” (Read/Write. Preserves file content by writing to the end of the file)
  • “x” (Write only. Creates a new file. Returns FALSE and an error if file already exists)
  • “x+” (Read/Write. Creates a new file. Returns FALSE and an error if file already exists)

Once you open the file using fopen() it will return a pointer ($file) to the right location in the file from where you can start reading and writing.

Now that the file is open you can read from it. There are many easy ways to read from a file. Normal is to read files on a line by line basis till we HIT the last character in the file which is called EOF (-1) = End Of File.

This code will print 1 line form the file and will close the file.

<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file); ?>

This code will print all the lines from the file and close it:

<?php $file = fopen("test.txt","r"); while(! feof($file))
{
  echo fgets($file). "<br />";
} fclose($file);
?>

What we do above is we go in a loop and read a line and print it and move ahead in the file line by line. feof() function will check if the file pointer $file points to EOF? If it does not that means we have not hit the end of the file so we can read in one more line.
More here: http://www.w3schools.com/php/func_filesystem_fgets.asp

For writing in a file there are many functions too. Here is one easy way to do it using fputs() function:

<?php
$file = fopen("test.txt","w");
echo fputs($file,"Hello World. Testing!");
fclose($file);
?>

You should always close the file once you have finished operating on it using the fclose() function.

What happens on a HTTP request?

So lets look behind the scenes and see what exactly goes on when you type www.yahoo.com in your browser?

  1. Your browser will first check if it knows the IP address to which yahoo.com domain matches? This domain-IP mapping can be stored in the browser cache when the browser looks it up for the first time.
  2. If the browser cannot find the domain-IP mapping in it’s cache, it will send a DNS request to the ISPs DNS server. The DNS server will lookup and find the IP mapping for www.yahoo.com. It will then respond with the IP address.
  3. The browser will get the IP address from DNS server and send the HTTP request for www.yahoo.com to that IP address.
  4. The server running at that IP address should be hosting the domain yahoo.com.
  5. The server will look at the incoming request. If the incoming request is a request for an HTML page it will serve it directly back to the client.
  6. If the request is for a PHP page, then the request will be sent to the PHP engine/module running on the server. The PHP engine/module will execute the PHP code and generate an HTML page will it will be give back to the HTTP server.
  7. The HTTP server will then send this HTML response back to the client’s browser.
  8. The browser will then render yahoo.com webpage for the client.

Looking behind the scenes always helps to get a better understanding and complete picture of how things work.

What is PHP?

Did you know that PHP originally meant - Personal Home Page? Yes thats right :) In 1994 Rasmus Lerdorf created PHP to track the online visitors coming to see his resume.

PHP, slowly grew in popularity and later came to be know as : PHP Hypertext Preprocessor.

Here are a few takeaways about PHP:

  • It can be embedded directly into the HTML pages to make them more dynamic.
  • It was designed to be a server-side scripting language more then a programming language. We say it is “server side” beacause when an HTTP request is made for a PHP page, all the logic inside the PHP page is executed on the server-side and finally an HTML page returned back to the user.
  • PHP is also cross-platform scripting language. That means, code written in PHP can be executed on Windows, UNIX and various flavors of LINUX.

Hope this helps!

Posted in Basics. Tags: , . No Comments »