Wednesday, June 6, 2012

PHP tutorial for beginners

Hypertext Pre-processor: What is PHP?

What is PHP?

Hypertext Pre-processor  (PHPs) is a  server-side scripting language, and  server-sidescripts are special commands you must place in Web pages. Those commands are processed before the pages are sent from your Server to the Web browser of your visitor. A typical PHP files will content commads to be executed in the server in addition to the usual mixture of text and HTML (Hypertext Markup Language) tags.

When you type a URL in the Address box or click a link on a Web page, you're asking a Web server on a computer somewhere to send a file to the Web browser (sometimes called a "client") on your computer. If that file is a normal HTML file, it looks exactly the same when your Web browser receives it as it did before the Web server sent it. After receiving the file, your Web browser displays its contents as a combination of text, images, and sounds. In the case of an PHP page, the process is similar, except there's an extra processing step that takes place just before the Web server sends the file. Before the Web server sends the HTML file to the Web browser, it runs all server-side scripts contained in the page. Some of these scripts display the current date, time, and other information. Others process information the user has just typed into a form, such as a page in the Web site's guestbook

To distinguish them from normal HTML pages, PHP files are usually given the ".php" extension.

What Can You Do with PHP?

There are many things you can do with PHP.
  • You can display date, time, and other information in different ways.
  • You can make a survey form and ask people who visit your site to fill it out, send emails, save the information to a file, etc

What Do PHP pages Look Like?

The appearance of an PHP page depends on who or what is viewing it. To the Web browser that receives it, an Active Server Page looks just like a normal HTML page. If a visitor to your Web site views the source code of an PHP page, that's what they see: a normal HTML page. However, the file located in the server  looks very different. In addition to text and HTML tags, you also see server-side scripts. This is what the PHP page looks like to the Web server before it is processed and sent in response to a request.

What Do PHP pages Look Like?

Server-side scripts look a lot like HTML tags. However, instead of starting and ending with lesser-than ( < ) and greater-than ( > ) brackets, they typically start with <?php or <? and will typically end with ?>. The <?php or <? are called anopening tags, and the ?> is called a closing tag. In between these tags are the server-side scripts. You can insert server-side scripts anywhere in your Web page--even inside HTML tags.

Do You Have to Be a Programmer to Understand Server-Side Scripting?

There's a lot you can do with server-side scripts without learning how to program. For this reason, much of the online Help for PHP is written for people who are familiar with HTML but aren't computer programmers.

This article aims to serve for beginner PHP developers but those who are comfortable with PHP will also find this article useful. Basically, I’ll tell you the difference between some of PHP commonly used functions/features. By the end of this article you will have a clear difference among:

  • include VS require
  • include_once VS include
  • require_once VS require
  • echo VS print
  • Single quote VS Double quote

Apparently all functions in each above point work in similar fashion. However, they are slightly different from other. Continue reading to explore the differences.

include VS require

Both are frequently used to include other files into our PHP code. Following are the differences between ‘include’ and ‘require’:

  • ‘include’ throws a warning, if your specified file is not found. Rest of the code will be rendered.
  • ‘require’ throws a fatal error, if your specified file is not found. Rest of the code will not be rendered.

Testing with Code Example

I’m using some code to make it more clear. To check it yourself you may follow the steps with me. Create a new file  ‘inc.file.php’, put below code in it:

1
2
3
4
5
6
7
8
<?php
function printMe()
{
 return "I have some code <br>";
}
 
echo printMe();
?>

Save the file in any directory of server. Now create a new file ‘index.php’ and put the below code in it:

1
2
3
4
<?php
include 'inc.nofile.php'; //File does not exist
include 'inc.file.php'; //This line will be rendered
?>

Save the file in same directory. To see how ‘include’ works, run the ‘index.php’ file from your browser. You will see an error something similar to:

Warning: include(inc.nofile.php) [function.include]: failed to open stream: No such file or directory in…

But still you can see that the text – “I have some code” is printed to the screen.

However, same test for ‘require’, shows that second line will not be rendered. Here is the code for testing ‘require’. Just copy and paste below code to your ‘index.php’ file (Remove any existing code)

1
2
3
4
<?php
require 'inc.nofile.php'; //File does not exist
include 'inc.file.php'; //This line will not be rendered
?>

This time we again get an error, but note that nothing is rendered after an error occurred.

Conclusion

Use ‘include’ when you want to render the code even if an error occurs. And use ‘require’ when you do not want to render the remaining code after an error occurs.

include_once VS include AND require_once VS require

‘include_once’ and ‘require_once’ are same in nature. Here we will see the difference of ‘include_once’ against ‘include’. Following are the differences between ‘include’ and ‘include_once’:

  • All includes for same file will be rendered, if you are using ‘include’.
  • While all includes for same file will be rendered only once, if you are using ‘include_once’. This helps you to prevent an error, if same file included again.

Testing with Code Example

To start off with experimenting, replace below code in ‘index.php’

1
2
3
4
<?php
include 'inc.file.php';
include 'inc.file.php'; //It will generate a fatal error
?>

Above code will be ended with following error,

Fatal error: Cannot redeclare printMe() (previously declared

Because, we are using same file in our includes. And our file – inc.file.php – has a function of printMe(). Since, we cannot re-declare a function in PHP, it throws an error.

But if we test below code by replacing it in our ‘index.php’ file, it will not throw any error.

1
2
3
4
<?php
include_once 'inc.file.php';
include_once 'inc.file.php'; //This will be ignored
?>

Since ‘inc.file.php’ is included using ‘include_once’, it will ignore the second line because it includes same file only once.

Conclusion

Use ‘include_once’, where you are in doubt of including the same file again somewhere in your code.

print VS echo

‘print’ and ‘echo’ are almost similar. Both are used to write text to screen. But there is a single difference between them:

  • ‘print’ does not accept multiple parameters.
  • While ‘echo’ accepts multiple parameters.

Above difference may be confusing to many people. I try to clear the confusion by explaining the difference by code.

Testing with Code Example

Let’s begin with ‘echo’ example. We pass two parameters to ‘echo’:

1
2
3
<?php
echo ("Tech "), ("Mug");
?>

Above code will be rendered without any error. But if we try same thing with ‘print’, it will be ended with a parse error. Let’s try it:

1
2
3
<?php
print ("Tech "), ("Mug"); //This will generate an error
?>

Above code will give you an error something similar to,

Parse error: parse error in…

Conclusion

We may pass multiple parameters to ‘echo’ but not to ‘print’.

Single quote VS Double quote

This might be very confusing for many developers to choose between single and double quote for writing string. There is a couple of difference between using single and double quote:

  • Any variable or special character like n r etc under the single quote will not be rendered. It will be displayed as an string.
  • Any variable or special character like n r etc under the double quote will be rendered properly.

Test with Code Example

Test the below code in ‘index.php’ file and run the file.

01
02
03
04
05
06
07
08
09
10
11
<?php
$var = 'I am a string!';
 
echo 'I write - $var'; //It will not render $var variable
echo "I write - $var"; //It will render $var successfully
 
// Special formatting characters (n r etc) also do not
// work with single quote
echo 'My first line n my new line'; // n sequence will not be rendered and displayed as a string
echo "My first line n my new line"; // n sequence will be rendered
?>

Explanation of above code should be understandable by reading the comments, the code has.

Conclusion

It does not make any difference to use single or double quote until you want to render a variable and/or any special character in it.

Hopefully, now you have a better understanding of above discussion and it helps for php beginner developers . Please feel free to share your thoughts or ask any question. Your comments are always welcome. If you like this article, please share it on your favorite website. It may help someone somewhere!


No comments:

Post a Comment

There was an error in this gadget