Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP-Basic By- Tanvi Raval. Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript.

Similar presentations


Presentation on theme: "PHP-Basic By- Tanvi Raval. Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript."— Presentation transcript:

1 PHP-Basic By- Tanvi Raval

2 Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript

3 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

4 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"

5 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

6 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

7 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

8 Basic PHP Syntax A PHP scripting block always starts 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.

9 –standard tags “ ”; –short tags “ ”; –ASP tags “ ”; –script tags “ ”.

10 <?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: <?php echo "Hello World"; ?>

11 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.

12 Comments in PHP In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <?php //This is a comment /* This is a comment block */ ?>

13 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; ?>

14 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.

15 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)

16 PHP Test <?php echo " Hello World “,”Tanvi”; echo phpinfo(); ?>

17 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.

18 How PHP code is Parsed

19 What happens to HTML Pages?

20 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.

21 What happens to PHP pages?

22 ❑ 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.

23 Embedding PHP and HTML Following is an example of how PHP is embedded: My first PHP Page This is normal HTML code <?php // php code goes here ?> Back into normal HTML

24 PHP will only process things that are enclosed within one of its valid code blocks (such as ). 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?

25 <?php $var = 5; ?> $var = 10; The variable $var has a value of: <?php =$var ?>

26 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.

27 Embedded conditionals... HTML CODE...... HTML CODE...... HTML CODE...

28 ... HTML CODE...

29 <?php $n=-2; if($n==0): ?> zero......... <?php endif; if($n>0):?> positive.... <?php endif; if ($n Nage.......

30 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

31 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.

32 Valid decimal integers: 1 123 +7 -1007395 Valid octal integers: 01 0123 +07 -01007345 Valid hexadecimal integers: 0x1 0xff 0x1a3 +0x7 -0x1ab7345

33 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.

34 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

35 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'

36 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.

37 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).

38 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

39 $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

40 <?php $a=array(‘xyz’=>38,2=>23); /*$a[1] = "32"; $a['Quagmire'] = "30"; $a['Joe'] = "34";*/ echo "Peter is ". $a[2]. " years old."; ?>

41 "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?>

42 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.

43 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: It means that $a and $b point to the same variable.

44 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: won't unset $b, just $a.

45 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.

46 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.

47 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.

48 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.

49 Operators Arithmetic : +,-,/,%,* Assignment operators for all above operators. +=, -= etc, ++, -- 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”; }

50 <?php $a=10; $b=10; if($a===$b) { print " Identical variables "; } ?>

51 Example Name Result $a == $b Equal TRUE if $a is equal to $b. $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only) $a != $b Not equal TRUE if $a is not equal to $b. $a <> $b Not equal TRUE if $a is not equal to $b. $a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only) $a < $b Less than TRUE if $a is strictly less than $b. $a > $b Greater than TRUE if $a is strictly greater than $b. $a <= $b Less than or equal to TRUE if $a is less than or equal to $b. $a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.

52 Global Variables a global variable can be accessed in any part of the program. However, in order to be modified, a global variable must be explicitly declared to be global in the function in which it is to be modified. This is accomplished, conveniently enough, by placing the keyword GLOBAL in front of the variable that should be recognized as global.

53 Placing this keyword in front of an already existing variable tells PHP to use the variable having that name. Consider an example: <?php $somevar = 15; function addit() { GLOBAL $somevar; $somevar++; print "Somevar is $somevar"; }

54 function addit1() { GLOBAL $somevar; $somevar++; print "Somevar is $somevar"; } addit(); addit1();?> O/P:- Somevar is 16

55 Static variables A static variable retains its value between calls to a function but is visible only within that function. You declare a variable static with the static keyword. For example: function update_counter

56 <?php function update_counter ( ) { static $counter = 0; $counter++; echo "Static counter is now $counter"; } $counter = 10; update_counter( ); echo “counter is $counter"; ?>

57 O/P:- Static counter is now 1 Static counter is now 2 Global counter is 10


Download ppt "PHP-Basic By- Tanvi Raval. Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript."

Similar presentations


Ads by Google