140 likes | 169 Views
Learn the basics of PHP scripting language, how to write PHP scripts, use variables and operators, and create dynamic web content.
E N D
PHP • PHP is a scripting language used for server side processing on Web servers. • PHP combines ease of use with sophisticated capability. • PHP lets you provide dynamic Web content, that is content that can automatically change from day to day or even minute to minute. • PHP is able to access files, databases, and other resources on the Web servers.
PHP • PHP scripts can be written using Notepad or any other text editors. • You can also use special HTML editors such as Dreamweaver MX which provide syntax coloring and other features that aid PHP programmers.
PHP • echo statement: • A PHP statement that sends output to a Web browser so a user can view it. • echo(“put some text here”); • HTML statements can be included in echo statements: • Echo(“<H2>A Likely Story</H2>”); • Note: All PHP statements end with a semicolon.
PHP • PHP statements are enclosed in <? … ?> <? // example-script.php // This script prints a message the user can read. echo(“This is a very simple script”); <? • The statements with the “//” are comments. Comments can also be enclosed like the following: /* this is a multi-line comment. It can consist of as many lines as you like. */
PHP • Variables in PHP: • Start with a dollar sign ($) • Follow the dollar sign with a letter or underscore ( _ ). The letter can be uppercase or lowercase. • Continue by adding as many letters, digits, or underscores as you like. • Variables in PHP are case sensitive Examples: $winner, $Winner, $highest_score, $x
PHP Operators + Addition • Subtraction * Multiplication / Division % Modulus ++ Increment -- Decrement . Concatenation
Conditional Operators a<b true if a is less than b a>b true if a is greater than b a<=b true if a is equal to or less than b a>=b true if a is equal to or greater than b a==b true if a is equal to b in value a!=b true if a not equal to b a===b true is identical (same type & value) a!==b true is not identical
Conditional Operators • x AND y true if both x and y are true • x && y true if both x and y are true • x OR y true if either or both are true • x || y true if either of both are true • x XOR y true if exactly one is true • !x true is x is false
Accessing PHP Pages • PHP pages must be executed on your Web Server. • On the Auburn Server, if your webpage is named “firstprogram.php” and you placed it in the root directory of your Web Site at www.auburn.edu/~smithjr, you would access the php page with: http://www.auburn.edu/~smithjr/firstprogram.php
A Simple PHP Program: <html> <head> <title> Generating HTML from PHP </title> </head> <? print ("Using PHP has <i>some advantages:</i>"); echo ("<ul> <li>Speed</li> <li>Ease of use</li> <li>Functionality</li> </ul>"); print ("</body> </html>"); ?>
String Variables • Manipulating string variables is an important task in any programming language. • The Concatenate Operator in PHP is the “.” (dot) sign. • $firstname.$lastname would concatenate the content of the variable “$firstname” with the content of the variable “lastname.”
String Functions • The strlen() function: Returns the number of characters in the string variable, e.g., $len = strlen($firstname); would return the number of characters in the variable $firstname. • The trim() function: Removes any blank characters from the beginning and end of a string, e.g., $name= trim($name);
String Functions • The strtolower() and strtoupper() functions: Return the input string in all lowercase or all uppercase, respectively. • The substr() function: Extract a portionof the characters in a string variable, e.g., $part=substr($name, 0, 5); returns five characters starting at position 0, that is, characters 0 through 4.