PHP Tutorial
Hypertext Preprocessor or PHP as it is generally known, is a popular, Open Source all-purpose scripting language that is suited for Web development and embedded into HTML. PHP differs from Perl or C scripting languages by using special start and end tags that enclose the code, allowing the programmer to freely move in and out of “PHP mode”. PHP is distinguishable from client-side JavaScript because the code is executed on the server. The client receives the script results without being able to determine the underlying code. Configuring the web server to process all HTML files with PHP allows for safer and more secure scripting possibilities.
PHP is a simple and easy to use scripting language for a beginner developer but offers professional programming options and advanced features. PHP is focused on server-side scripting but has many more possibilities within designing, building, testing and deploying PHP sites. The syntax of PHP is similar to C, Java and Pearl and allows the web developer to create code for dynamically generated web pages in a quick and simple manner.
Although the main target field for PHP is server-side scripting, PHP can also be used for command line scripting and writing desktop applications. Tutorials include overviews and specific information on file manipulation, arrays, strings, functions, operators, variables, data types and installation for PHP.
PHP is versatile and can be used with Linux, Unix, Microsoft Windows, Mac OS X and other operating systems. PHP supports many web servers and the developer can choose whether to use PHP as a procedural programming or object-oriented programming tool.
PHP Tutorial – Introduction In this tutorial you will learn about PHP – Introduction to PHP, What you should already know? What’s PHP? What’s the difference between PHP and HTML? When to use PHP? What makes PHP a choice among the other scripting languages? What you should already know? You have to have good knowledge of the following before you can proceed: HTML JavaScript What’s PHP? PHP stands for Hypertext Preprocessor. PHP scripts run inside Apache server or Microsoft IIS. PHP and Apache server are free. PHP code is very easy. PHP is the most used server side scripting language….
PHP Tutorial – Installation In this tutorial you will learn about PHP – Installation steps involved in Installing Apache server, Installing PHP 4 and Installing PHP 5. Installing Apache server: 1- Download the installer from Apache site: http://httpd.apache.org/download.cgi 2- Double-click on the installer file to start the installation process. 3- You will get a welcome screen, select Next to continue the installation process. 4- You will be prompted to accept the Apache license. 5- After you accept the license, the installer presents you with a brief introduction to Apache. 6- Then the installer will ask you to provide basic information…
PHP Tutorial – Syntax In this tutorial you will learn about PHP Syntax – Syntax for Writing a script, Scripts VS. File and Comments Writing a script: To embed PHP code inside a file, it has to be inside a special set of opening and closing tags. PHP supports the following tags sets: 1. Opening (< ?php) and closing (? >) 2. Opening (< ?) and closing (? >) 3. Opening (< %) and closing (% >) 4. Opening (< script language=”php” >) and closing (< /script >) Example: < html > …..< head > ……….< title >Sample document< /title…
PHP Tutorials – Data Types In this PHP Tutorial you will learn about PHP Data Types viz. Numeric values, String values, Boolean values, Arrays, Objects and NULL. Numeric values: There are two numeric values in PHP, they are: 1. integer: Integer numbers don’t have floating point, for example, 5, 12, 1567 2. real: or floating point numbers, real number has a floating point, for example, 2.3 There are three ways to represent the numeric values: 1. base 10 numbers: They are represented using digits from 1 to 9, with an optional (.) for floating point if the value is real. 2….
September 18, 2006
-
Exforsys
- Comments:
PHP Tutorials – Operators (Part I) In this PHP Tutorial you will learn 1st Part of Operators – The assignment operator, The arithmetic operators, Bitwise operators – AND (&), OR (|), XOR (^), NOT (~) and Error control operator The assignment operator: It’s used to assign a value to a variable, for example, to assign the value 100 to the variable $var: $var = 100; The variable comes at the left of the operator, and the value at the right, the value can be another variable, and in this case the variable at the left will be assigned to the…
September 18, 2006
-
Exforsys
- Comments:
Variables In this PHP Tutorial you will learn about Variables, different types of Variables like Identifiers, Variables, Statements and Constants.Variables In this PHP Tutorial you will learn about Variables, different types of Variables like Identifiers, Variables, Statements and Constants. Identifiers: Identifier is the name of the data, identifiers also can be used as a label for a set of commands. There are rules for identifiers naming, rules are: • The first character has to be a letter or an underscore (_); • The following characters can be any combination of, letters, digits, and undersore. Example: The following are valid identifiers:…
September 22, 2006
-
Exforsys
- Comments:
PHP Tutorials – Operators In this PHP Tutorial you will learn about Operators – String concatenation operator, Comparison operators, Logical operators, Type casting, Combined assignment operator, Operators precedence and Operators precedence order. String concatenation operator: The string concatenation operator (.) concatenates two strings. Example: <?php $s1 = “String1”; $s2 = $s1 . “ and String2 are concatenated.”; ?> Comparison operators: Comparison operators are binary operators, they compare between two operands of any type, and return a Boolean value: == to check if two operands are equal. != to check if two operands are not equal. < to check…
September 22, 2006
-
Exforsys
- Comments:
PHP Tutorials – Conditional Statements In this PHP Tutorials you will learn about Conditional Statements – if statement, if-else statement, Alternative if-else statement and switch statement. if statement: if syntax is as follows: if (an expression that return Boolean value) { Code to be executed. } PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the curly braces is executed, else it will be ignored. Example: <?php $x = 5; if($x < 10) { // returns true echo $x; // Will be executed }…
September 25, 2006
-
Exforsys
- Comments:
PHP Tutorials – Functions (Part I) In this PHP Tutorial you will learn about PHP Functions – Part 1, Function syntax, example, parameters and variable scope. Sometimes you need to perform an operation more than once in the code, to do so, you can use functions, A function is declared using the following syntax: function function_name([parameters]) The name of the function must follow the rules of the identifier, because it’s an identifier that identifies this function. Function can have zero to many parameters, parameters are used as variables thought the function code block. The function code block starts with a curly…
September 29, 2006
-
Exforsys
- Comments:
PHP Tutorials – Loops In this PHP Tutorial you will learn about Loops – while loop, do-while loop and the for loop along with syntax and sample PHP loop codes. while loop: A while statement executes a code block until a condition is set. Example: <?php $x = 3; while($x != 0) { echo $x; $x–; } ?> The while block will be executed three times, and the values 3, 2, and 1 will be printed, at then the value of $x will be zero, which causes the loop to exit. do-while loop:…
September 30, 2006
-
Exforsys
- Comments:
PHP Tutorials – Functions (Part-2) In this PHP Tutorial 2nd Part of PHP Functions you will learn about Variable parameters – func_num_args(), func_get_args(), func_get_arg($arg_num), Variable variables and variable functions, Some useful functions – phpinfo() and header() Variable parameters Sometimes you may not know the number of parameters to be passed for a function, for example if you want to make a reusable function that calculates the sum of some numbers, and you want it to be able to calculate any number of numbers, to do so, you need to use the variable parameters. Before you know how to use the…
PHP Tutorials – Strings (Part I) In this PHP Tutorial you will learn about Strings viz The heredoc syntax, String length, String position, Strings comparison, String search and Substring selection The heredoc syntax: PHP has another way to specify strings called heredoc syntax. The heredoc syntax is very useful for specifying large text. The heredoc syntax is very useful for specifying text that contains quotes and double quotes, because they don’t need to be escaped. Heredoc syntax starts with a <<< and a label which is an identifier, and ends with the identifier label, for example: echo <<<NAMEFORM …
PHP Tutorials : Strings (Part 2) In this PHP Tutorial you will learn about Strings (2nd Part) – String cleanup, String replacement, Case functions, String formatting and A list of the available type specifiers. String cleanup: String cleanup is removing \n, \r, \t, \0, and spaces from any side of the string, all cleanup functions take one argument, which is the string to be cleaned, and returns a new value, which is the cleaned string. To clean up the end of the string use the function rtrim() or chop(). To clean up the start of the string use the function…
Regular expressions In this PHP Tutorials you will learn about Regular Expressions viz Basic PCRE Syntax, Character classes, preg_match(), preg_match() and Extracting data with regular expressions. Basic PCRE Syntax: A regular expression pattern is a string consisting of plain text and pattern metacharacters. The regexp metacharacters define the type and number of characters that can match part of a pattern. Character classes allow a pattern to match multiple characters simultaneously. Character classes are: d Digits 0–9 D Anything not a digit w Any alphanumeric character or an underscore (_) W Anything not an alphanumeric character or an underscore s Any…
PHP Tutorials : Arrays In this PHP Tutorial you will learn about Array basics, Creating an array, Creating a numeric array and Finding the Size of an Array. Array basics: An array is made up of elements. Each element has a key and a value. An array holding information about the GPAs of a student has courses names for keys and GPAs for values: Course GPA Introduction to computer A Introduction to programming B+ Discrete structure B Computer organization A- An array can only have one element with a given key. In the GPA array, there can’t be another element…
PHP Tutorials – Arrays (Part 2) In this PHP Tutorial you will learn Arrays (Part 2) – Looping with foreach( ), Sorting arrays and Using Multidimensional Arrays. Looping with foreach( ): The easiest way to iterate through each element of an array is with foreach( ). The foreach( ) construct lets you run a code block once for each element in an array. Example: $fruits = array(‘apple’, ‘banana’, ‘orange’); print “<table>\n”; foreach ($fruits as $fruit) { print “<tr><td>$fruit</td></tr>\n”; } print ‘</table>’; This will print: <table> <tr><td>apple</td></tr> <tr><td>banana</td></tr> <tr><td>orange</td></tr> </table> if you want to know what element you’re on as you’re iterating through…
November 15, 2006
-
Exforsys
- Comments:
PHP Tutorials – File manipulation (Part 1) In the PHP Tutorial You will learn about File manipulation (Part 1) – Checking file existence, A file or directory, Determining file size, Creating and deleting files and Opening a file for writing, reading, or appending. Checking file existence: You can test for the existence of a file with the file_exists() function. file_exists() takes one element, which is a string representing an absolute or relative path to a file that might or might not be there. If the file is found, file_exists() returns true; otherwise, it returns false. Example: if (file_exists(“file.jpg”)) { print…
November 15, 2006
-
Exforsys
- Comments:
File manipulation (Part-2) In this PHP Tutorial you will learn the 2nd Part of File Manipulation Reading lines from a file, Reading arbitrary amounts of data from a file, Writing to a file, Creating directories, Removing a directory and Opening a directory for reading. Reading lines from a file: To read a line from an open file, you can use fgets(). fgets() requires the file resource returned from fopen() as an argument. You may also pass fgets() an integer as a second argument. The integer argument specifies the number of bytes that the function should read if it doesn’t first…
November 22, 2006
-
Exforsys
- Comments:
PHP Tutorials – Forms In the PHP Tutorial you will learn about PHP Forms – Predefined variables, Reading input from forms and Using hidden fields to save state. Predefined variables: PHP has several predefined variables called superglobals. Superglobals are always present and available in any PHP script. The superglobals are arrays of other variables. PHP superglobals that work with forms: • $_GET contains any variables provided to a script through the GET method. • $_POST contains any variables provided to a script through the POST method. • $_FILES contains any variables provided to a script through file uploads. Reading…
November 22, 2006
-
Exforsys
- Comments:
PHP Tutorial : PHP & MySQL In this PHP Tutorial you will learn about PHP and MySQL – Connecting to MySQL, Closing a connection, Selecting a database, Executing a query, Inserting data and Retrieving data. Connecting to MySQL: To connect to MySQL database server, use the function mysql_connect(). mysql_connect() takes three string arguments, the hostname, the username, and the password. mysql_connect() returns an integer represents the connection index if the connection is successful, or returns false if the connection fails. Example: <?php $con = mysql_connect(“localhost”, “john”, “smith”); echo $con; ?> Closing a connection: To close a connection to MySQL, use the…
WAMP SERVER WAMP is an acronym for Windows, Apache, MySQL and PHP. It is a combination of independently created software’s bundled together into a SINGLE installation package to set up a SERVER on your machine with out any hassles. It comes with the GPL License. Apache HTTP Server: Widely know as Apache Server is the most widely used webserver out there. Apache is developed and maintained by an open community of developers under the guidance of the Apache Software Foundation. It is available for wide variety of operating systems like Windows, Mac OS X, Linux, Unix, FreeBSD etc. MySQL: It…
This tutorial will demonstrate how to implement a user login/membership system in your website. Though implementing login/membership system appears to be a herculean task, it is actually lot more simpler than you think. In fact, all you need is a single table in your database, and a PHP-supported Server. To begin with, here is an outline of what this tutorial will show you how to do: User registration Gather username, password, email, and store in table User login Before we can begin, we need to do the backend work with MySQL. First you can create a database, and in this…
What is a Cookie? A cookie is flat file based system used to represent a user of the website. It is stored on the local computer of the user. When you visit a website a cookie may be set to represent the user. Next time he visits the website, he does not need to identify himself manually; instead the cookie will represent him on that website. With the help of PHP, cookies can be created, read and destroyed. The main difference between a session and a cookie is that cookies are stored on the user’s computer where as sessions are…
This tutorial shows how to handle strings in PHP, different output methods, functions and the significance of using each of them. A string can be literally expressed in the following ways in PHP 1. Single Quoted 2. Double Quoted 3. HEREDOC 4. NOWDOC (from ver. PHP 5.3.0) Different methods used to handle strings are: 1. Print 2. Echo 3. Sprintf 4. Print_r 5. Var_dump Single Quoted String: The simplest way to specify a string is to enclose it in single quotes (the character ‘). To specify a single quote in your string you will need to escape it with a…