Monday, February 11, 2008

PHP Programmer - Amit Shah

Based on my Experience here I am sharing some basic points of PHP which can help you out to know about PHP.



By Amit Shah - PHP Programmer

PHP, MySQL, java script, AJAX, Programming, Web Development Services by - Amit Shah a PHP Programmer



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

Variables in PHP


  • Variables are used for storing a values, like text strings, numbers or arrays.

  • When a variable is declared, it can be used over and over again in your scrilit.

  • All variables in PHP start with a $ sign symbol.

  • The correct way of declaring a variable in PHP:

  • PHP is a Loosely Typed Language


  • In PHP, a variable does not need to be declared before adding a value to it.

  • In the example above, you see that you do not have to tell PHP which data type the variable is.

  • PHP automatically converts the variable to the correct data type, depending on its value.

  • In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.

  • In PHP, the variable is declared automatically when you use it.

Naming Rules for Variables


  • A variable name must start with a letter or an underscore "_"

  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )

  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)

String Variables in PHP


  • String variables are used for values that contains characters.

  • In this chapter we are going to look at the most common functions and operators used to manipulate strings in PHP.

  • After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.

  • Below, the PHP script assigns the text "Hello World" to a string variable called $txt:

  • The Concatenation Operator


  • There is only one string operator in PHP.

  • The concatenation operator (.) is used to put two string values together.

  • To concatenate two string variables together, use the concatenation operator:

Conditional Statements


  • Very often when you write code, you want to perform different actions for different decisions.

  • You can use conditional statements in your code to do this.

  • In PHP we have the following conditional statements:


  • if statement - use this statement to execute some code only if a specified condition is true

  • if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false

  • if...elseif....else statement - use this statement to select one of several blocks of code to be executed

  • switch statement - use this statement to select one of many blocks of code to be executed

What is an Array?


  • You have already learnt that a variable is a storage area holding numbers and text. The problem is, a variable will hold only one value.

  • An array is a special variable, which can hold more than one value, at a time.

  • If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

  • An array can hold all your variable values under a single name. And you can access the values by referring to the array name.

  • Each element in the array has its own index so that it can be easily accessed.

  • In PHP, there are three kind of arrays:


  • Numeric array - An array with a numeric index

  • Associative array - An array where each ID key is associated with a value

  • Multidimensional array - An array containing one or more arrays

Numeric Arrays



  • A numeric array stores each array element with a numeric index.

  • There are two methods to create a numeric array.

  • 1. In the following example the index are automatically assigned (the index starts at 0):

Associative Arrays


  • An associative array, each ID key is associated with a value.

  • When storing data about specific named values, a numerical array is not always the best way to do it.

  • With associative arrays we can use the values as keys and assign values to them.

Multidimensional Arrays


  • In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

PHP Loops


Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
In PHP, we have the following looping statements:

  • while - loops through a block of code while a specified condition is true

  • do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true

  • for - loops through a block of code a specified number of times

  • foreach - loops through a block of code for each element in an array

PHP Functions


  • In this chapter we will show you how to create your own functions.

  • To keep the browser from executing a script when the page loads, you can put your script into a function.

  • A function will be executed by a call to the function.

  • You may call a function from anywhere within a page.

PHP Functions - Adding parameters



  • To add more functionality to a function, we can add parameters. A parameter is just like a variable.

  • Parameters are specified after the function name, inside the parentheses.