570 likes | 589 Views
PHP-Basic. By- Tanvi Raval. Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript. What is PHP?. PHP stands for P HP: H ypertext P reprocessor PHP is a server-side scripting language, like ASP
E N D
PHP-Basic By- Tanvi Raval
Pre-requisites • Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript
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
Why PHP? • 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. • 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
PHP Installation • What do you need? • Most people would prefer to install a all-in-one solution: • WampServer 2.0i for Windows platform Includes : - Apache 2.2.11 - MySQL 5.1.36 - PHP 5.3.0 • http://www.wampserver.com/en/ • http://lamphowto.com/ for Linux platform
Basic PHP Syntax • A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document. • On servers with shorthand support enabled you can start a scripting block with <? ?>. • For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.
standard tags “<?php ?>”; • short tags “<? ?>”; • ASP tags “<% %>”; • script tags “<SCRIPT LANGUAGE=”php”> </SCRIPT>”.
<?php ?> • A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. • Below, we have an example of a simple PHP script which sends the text "Hello World" to the browser: • <html> <body> <?php echo "Hello World"; ?> </body> </html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another. • There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello World". • Note: The file must have a .php extension. If the file has a .html extension, the PHP code will not be executed.
Comments in PHP • In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. • <html> <body> <?php //This is a comment /* This is a comment block */ ?> </body> </html>
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 script. • All variables in PHP start with a $ sign symbol. • The correct way of declaring a variable in PHP: • $var_name = value; • Let's try creating a variable containing a string, and a variable containing a number: • <?php $txt="Hello World!"; $x=16; ?>
PHP is a Freely 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)
<html> <head><title>PHP Test</title> </head> <body><?php echo "<p>Hello World</p>“,”Tanvi”; echo phpinfo(); ?> </body> </html>
A call to the phpinfo() function returns a lot of useful information about your system and setup such as available predefined variables, loaded PHP modules, and configuration settings.
When a request for a page comes from the browser, the web server performs three steps: ❑ Read the request from the browser. ❑ Find the page in the server. ❑ Send the page back across the Internet to the browser.
❑ Read the request from the browser. ❑ Find the page in the server. ❑ Perform any instructions provided in PHP to modify the page. ❑ Send the page back across the Internet to the browser.
Embedding PHP and HTML • Following is an example of how PHP is embedded: <html> <head> <title>My first PHP Page</title> </head> <body> This is normal HTML code <?php // php code goes here ?> Back into normal HTML </body> </html>
PHP will only process things that are enclosed within one of its valid code blocks (such as <?php and ?>). • Because of this, PHP effectively ignores everything that it was not specifically told to process and can be used to our advantage. • For example, what will the output from the following be?
For example, what will the output from the following be? <?php $var = 5; ?> $var = 10;<br /> The variable $var has a value of: <?php =$var ?> <br />
O/P: $var = 10; The variable $var has a value of: 5 • Notice that with the second assignment of $var, when we attempt to change the value from 5 to 10, it has no effect because it is not enclosed within valid PHP code-block syntax. • So, instead of being processed, it is simply displayed to the web browser.
Embedded conditionals • <?php if(conditions) { ?> ... HTML CODE ... <?php } ?> • <?php if(conditions): ?> ... HTML CODE ... <?php endif; ?> • <?php while(conditions) : ?> ... HTML CODE ... <?php endwhile; ?>
<?php for(init;conditions;increment) : ?> ... HTML CODE ... <?php endfor; ?>
<html> <body> <?php $n=-2; if($n==0): ?> zero......... <?php endif; if($n>0):?> positive.... <?php endif; if ($n<0){?> Nage....... <?php } ?> </body> </html>
PHP Data Types • A data type refers to the type of data a variable can store. PHP has eight (8) different data types you can work with. These are: 1.integer numbers 2.floating point numbers 3.strings 4.booleans 5.arrays 6.objects 7.resouces 8.null
Integers • An integer is a whole number. That is to say, it is a number with no fractional component. For those who are familiar with C, a PHP integer is the same as the long data type in C. It is a number between -2,147,483,648 and +2,147,483,647. • decimal: a base ten numbering system • Integers can be written in decimal, octal, or hexadecimal. Decimal numbers are a string of digits with no leading zeros. Any integer may begin with a plus sign ( + ) or a minus sign ( - ) to indicate whether it is a positive or negative number. If there is no sign, then positive is assumed.
Valid decimal integers: 1 123 +7 -1007395 • Valid octal integers: 01 0123 +07 -01007345 • Valid hexadecimal integers: 0x1 0xff 0x1a3 +0x7 -0x1ab7345
Floating Point Numbers • Floating-point numbers are also sometimes called real numbers. They are numbers that have a fractional component. Unlike basic math, all fractions are represented as decimal numbers. If you are familiar with C, PHP floating-point numbers are equivalent to the double data type. Floating-point numbers get their name from their decimal point. When using scientific notation to represent the number, the point floats in relation to the exponent being applied to the numeric component of the notation.
PHP recognizes two types of floating point numbers. The first is a simple numeric literal with a decimal point. The second is a floating-point number written in scientific notation. Scientific notation takes the form of [number]E[exponent], for example, 1.75E-2. • Some examples of valid floating point numbers include: 3.14 0.001 -1.234 0.314E2 // 31.4 1.234E-5 // 0.00001234 -3.45E-3 // -0.00345
Strings • A string is a text literal of an arbitrary length. Most of working with Web pages is about working with strings. A string is indicated by being enclosed in quotes, either double or single quotes. • $myVar = "xyz"; // assign a value to $myVar echo $myVar; // writes 'xyz' echo "abc to $myVar"; // writes 'abc to xyz‘ // but with single quotes echo 'abc to $myVar': // writes 'abc to $myVar'
Booleans • A boolean value assesses the truth value of something. Booleans only have two values, true and false. These two values are represented by the keyword literals of the same name. All conditionals return a true/false boolean value based on the condition being tested.
Object • An object is a data type that allows for the storage of not only data but also information on how to process that data. • PHP is capable of functioning as an object-oriented programming language (or OOP).
Arrays • An array is a variable that holds a group of values. • Arrays are usually meant to store a collection of related data elements, although this is not a necessity. • You access the individual elements by referring to their index position within the array. The position is either specificied numerically or by name
$arrName=array( key => value , ... ) // key may be an integer or string // value may be any value OR • $arr[key] = value; • $arr[] = value; // key may be an integer or string // value may be any value
<html> <body> <?php $a=array(‘xyz’=>38,2=>23); /*$a[1] = "32"; $a['Quagmire'] = "30"; $a['Joe'] = "34";*/ echo "Peter is " . $a[2] . " years old."; ?> </body> </html>
<?php$arr = array("foo" => "bar", 12 => true);echo $arr["foo"]; // barecho $arr[12]; // 1?>
NULL • Null is a special data type which can have only one value, which is itself. Which is to say, null is not only a data type, but also a keyword. • A variable of data type null is a variable that has no value assigned to it. • When a variable is created without a value, it is automatically assigned a value of null.
Resources OR References • References in PHP are a means to access the same variable content by different names. • PHP references allow you to make two variables to refer to the same content. Meaning, when you do: • <?php $a =& $b ?> • It means that $a and $b point to the same variable.
When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed. For example: • <?php$a = 1;$b =& $a;unset ($a); ?> • won't unset $b, just $a.
Variable Functions • 1. is_array():- Finds whether a variable is an array • Description:- is_array ( var) • Returns TRUE if var is an array, FALSE otherwise.
2. is_float() -- Finds whether a variable is a float • Description;- is_float (var) • Returns TRUE if var is a float, FALSE otherwise. 3. is_int() -- Find whether a variable is an integer • Description:- is_int (var) • Returns TRUE if var is an integer FALSE otherwise.
4. is_string() -- Finds whether a variable is a string • Description:-is_string (var) • Returns TRUE if var is a string, FALSE otherwise. 5. is_object() -- Finds whether a variable is an object • Description:- is_object (var) • Returns TRUE if var is an object, FALSE otherwise.
6. is_bool() -- Finds out whether a variable is a boolean • Description:- is_bool (var) • Returns TRUE if the var parameter is boolean. 7. is_null -- Finds whether a variable is NULL • Description:-is_null (var) • Returns TRUE if var is null , FALSE otherwise. • See the null type when a variable is considered to be NULL and when not.
Operators • Arithmetic : +,-,/,%,* • Assignment operators for all above operators. +=, -= etc , ++ , -- <?php$a = 3;$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;?> • Comparision == , != , <> , > , >= , < , <= , === === returns true if its two operands are having the same value, and they are of the same type. e.g. $a=15; $b=15; if( $a === $b) { print “Identical variables”; }
<html> <body> <?php $a=10; $b=10; if($a===$b) { print " Identical variables "; } ?> </body> </html>