Showing posts with label furqan. Show all posts
Showing posts with label furqan. Show all posts

Monday, February 28, 2011

PHP Useful Functions

Functions with Arbitrary Number of Arguments

You may already know that PHP allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments.
First, here is an example with just optional arguments:
// function with 2 optional arguments
function foo($arg1 = '', $arg2 = '') {

 echo "arg1: $arg1\n";
 echo "arg2: $arg2\n";

}

foo('hello','world');
/* prints:
arg1: hello
arg2: world
*/

foo();
/* prints:
arg1:
arg2:
*/
Now, let’s see how we can build a function that accepts any number of arguments. This time we are going to utilize func_get_args():
// yes, the argument list can be empty
function foo() {

 // returns an array of all passed arguments
 $args = func_get_args();

 foreach ($args as $k => $v) {
  echo "arg".($k+1).": $v\n";
 }

}

foo();
/* prints nothing */

foo('hello');
/* prints
arg1: hello
*/

foo('hello', 'world', 'again');
/* prints
arg1: hello
arg2: world
arg3: again
*/

Sunday, February 27, 2011

php tutorial part II

Introduction

In the last part of the tutorial I explained some of the advantages of PHP as a scripting language and showed you how to test your server for PHP. In this part I will show you the basics of showing information in the browser and how you can use variables to hold information.

Printing Text

To output text in your PHP script is actually very simple. As with most other things in PHP, you can do it in a variety of different ways. The main one you will be using, though, is print. Print will allow you to output text, variables or a combination of the two so that they display on the screen.

The print statement is used in the following way:

print("Hello world!");

I will explain the above line:

print is the command and tells the script what to do. This is followed by the information to be printed, which is contained in the brackets. Because you are outputting text, the text is also enclosed instide quotation marks. Finally, as with nearly every line in a PHP script, it must end in a semicolon. You would, of course, have to enclose this in your standard PHP tags, making the following code:



Which will display:

Hello world!

on the screen.

Variables

As with other programming languages, PHP allows you to define variables. In PHP there are several variable types, but the most common is called a String. It can hold text and numbers. All strings begin with a $ sign. To assign some text to a string you would use the following code:

$welcome_text = "Hello and welcome to my website.";

This is quite a simple line to understand, everything inside the quotation marks will be assigned to the string. You must remember a few rules about strings though:

Strings are case sensetive so $Welcome_Text is not the same as $welcome_text
String names can contain letters, numbers and underscores but cannot begin with a number or underscore
When assigning numbers to strings you do not need to include the quotes so:

$user_id = 987

would be allowed.

Outputting Variables

To display a variable on the screen uses exactly the same code as to display text but in a slightly different form. The following code would display your welcome text:



As you can see, the only major difference is that you do not need the quotation marks if you are printing a variable.

Formatting Your Text

Unfortunately, the output from your PHP programs is quite boring. Everything is just output in the browser's default font. It is very easy, though, to format your text using HTML. This is because, as PHP is a server side language, the code is executed before the page is sent to the browser. This means that only the resulting information from the script is sent, so in the example above the browser would just be sent the text:

Hello and welcome to my website.

This means, though, that you can include standard HTML markup in your scripts and strings. The only problem with this is that many HTML tags require the " sign. You may notice that this will clash with the quotation marks used to print your text. This means that you must tell the script which quotes should be used (the ones at the beginning and end of the output) and which ones should be ignored (the ones in the HTML code).

For this example I will change the text to the Arial font in red. The normal code for this would be:




As you can see this code contains 4 quotation marks so would confuse the script. Because of this you must add a backslash before each quotation mark to make the PHP script ignore it. The code would chang
e to:




You can now include this in your print statement:

print("Hello and welcome to my website.");

which will make the browser display:

Hello and welcome to my website.

because it has only been sent the code:

Hello and welcome to my website.

This does make it quite difficult to output HTML code into the browser but later in this tutorial I will show you another way of doing this which can make it a bit easier.

Saturday, February 26, 2011

Learn PHP from Scratch: A Training Regimen

Holding the title of “number one”, PHP is the most popular language among developers. Even still, many prefer different languages. Yours truly, for example, is most comfortable when working in the ASP.NET environment. However, because of the enormous success of WordPress, more and more developers have decided to expand their horizons and learn yet another language.
I happen to be one of those very people. As my clients increasingly ask for WordPress implementation, learning PHP has become a requirement. I’m not alone in this endeavor. For those in the same boat, why not take the time and learn with me?
The Mission Statement
Over the course of the next few articles – to be posted each Wednesday – it is my intention to create a “work-out regimen” for all of us. If you’ve been meaning to learn, but haven’t gotten around to it yet, now is the time! On the flip side, for those of you who are PHP ninjas, I respectfully request that you get involved and offer advice to the rest of us. If you’ve benefited from the dozens of tutorials on this site, take a few moments to give back, via the comments section. This will be your one-stop resource for everything PHP. Each Wednesday, I’ll post a training article, as well as a list of resources that will help to explain the concepts in the lesson further. The key here is that I’m a beginner just like everyone else, relatively speaking. We can motivate one another to learn as quickly, and efficiently as possible.
So, why would you learn from a beginner? Try not to think of it as me teaching you. Think of these articles as a community effort where we all help each other. I’ll be learning from many of you in the same way that you learn from me.
Is PHP Similar To Any Other Languages?
Absolutely. I was pleasantly surprised as I began my training. If you have even a modest amount of knowledge when it comes to ASP.NET, Perl, Javascript, or C#, you’ll find that you pick up the syntax quickly.
The Basics
In order to alert the server that we are working with PHP, you’ll need to use the following syntax when adding PHP into your HTML documents:
<?php 
...code goes here 
?> 
We start and end each PHP declaration with “<?php” and “?>”, respectively. Refer back to your code and insert the following:
1.    <?php echo "This is PHP in action"; ?> 
Notice that in this second example, we kept everything on one line. Remember, PHP is not white-space sensitive. Here, we are telling the server to “echo”, or write “This is PHP in action” onto the page. Each declaration in our code must have a semicolon appended to the end. Although HTML can be forgiving if you accidentally forget a bracket, PHP unfortunately is not. If you don’t use the correct syntax, you’ll receive an error. In this case, when we only have a single declaration, we could technically get away with leaving the semicolon off. But, it’s always important to follow best practices.
Defining Variables
We can assign values to variables quite easily. Rather than using “var” (C# and Javascript), or “dim” (VB), we can declare a variable in PHP by using the “$” symbol. For instance, let’s say that I want to assign the previous string to a variable called “myVariable”. I would write…
view plaincopy to clipboardprint?
1.    <?php $myVariable =  "This is PHP in action"; 
2.      echo $myVariable; 
3.    ?> 
This example will produce the exact same result as the previous two. However, in this scenario, we’ve assigned the string to the variable and then “echoed” the variable instead. Now what if I wanted to concatenate a variable and a string?
view plaincopy to clipboardprint?
1.    <?php $myVariable =  "This is PHP in action."; 
2.      echo $myVariable . " My name is Jeffrey Way"; 
3.    ?> 
By using the period, we can combine variables and/or strings.
Inserting Comments Into Your Code
If you’re familiar with CSS and Javascript, you’ll find that inserting comments in PHP is virtually the same.
<?php
  # This is a single line comment.
  // This is the most common way of commenting out your code.
  /* Here is a way to comment over multiple lines. This is the exact
     same way that you would comment in CSS */
?>
Combining HTML With Our PHP
As said previously, remember that PHP and HTML can work in combination. Simply because we’re in the middle of a PHP statement does not mean that we can’t embed elements such as a break or strong tag.
<?php echo "<strong>This text is bold.</strong>"; ?>
Defining Your First Function()
Creating functions in PHP is nearly identical to Javascript’s implementation. The basic syntax is…
<?php
function name ($arguments){
your statement goes here;
}
?>
If we wanted to create a function that “echos” 10 plus 5, we could write…
1.    <?php 
2.    function addNumbers (){ 
3.    echo 10 + 5; 
4.    } 
5.    addNumbers(); 
6.    ?> 
We’re creating a simple function that will output “15″. We call the function with “addNumbers(). In this case, we aren’t using any arguments. Let’s see how we can implement them in order to make our function more versatile.
1.    <?php 
2.    function addNumbers($firstNumber, $secondNumber){ 
3.    echo $firstNumber + $secondNumber; 
4.    } 
5.    addNumbers(10, 5); 
6.    ?> 
Now, our code is much more flexible. When we created our “addNumbers()” function, we added two arguments – $firstNumber and $secondNumber. The function will simply echo the sum of these two variables. When the function is called, we’ll need to pass in our two numbers – addNumbers(10, 5). In a real-world situation, the values for these variables might be taken from a couple of textboxes.

Thursday, February 24, 2011

First PHP Class

PHP is a powerful tool for making dynamic and interactive Web pages.
PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

What is PHP?

    * PHP stands for PHP: Hypertext Preprocessor
    * PHP is a server-side scripting language, like ASP
    * PHP scripts are executed on the server
    * PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
    * PHP is an open source software
    * PHP is free to download and use

What is a PHP File?

    * PHP files can contain text, HTML tags and scripts
    * PHP files are returned to the browser as plain HTML
    * PHP files have a file extension of ".php", ".php3", or ".phtml"

What is MySQL?

    * MySQL is a database server
    * MySQL is ideal for both small and large applications
    * MySQL supports standard SQL
    * MySQL compiles on a number of platforms
    * MySQL is free to download and use

PHP + MySQL

    * PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)

Why PHP?

    * PHP runs on different platforms (Windows, Linux, Unix, etc.)
    * PHP is compatible with almost all servers used today (Apache, IIS, etc.)
    * PHP is FREE to download from the official PHP resource: www.php.net
    * PHP is easy to learn and runs efficiently on the server side

Where to Start?

To get access to a web server with PHP support, you can:

    * Install Apache (or IIS) on your own server, install PHP, and MySQL
    * Or find a web hosting plan with PHP and MySQL support