Programming in PHP BASICSPART I Prepared by K
Programming in PHP BASICS-PART I Prepared by K. PONVEL AZHAGU LAKSHMI Assistant Professor of Computer Science Nehru Memorial College Puthanampatti
PHP Introduction �Php-Hyper Text Pre. Processor �General purpose, Open Source, server side scripting Language �Designed by : Rasmus Lerdorf in 1994 �Developer: Zend Technologies �Useful for developing websites 2 MCA/PDB 1/KPAL 7/30/2018
Php versions �PHP/FI- Personal Home Page/Form Interpreter. Collection of Perl Scripts �PHP/FI 2 -1997 �PHP 3 -1998: Hypertext Preprocessor. Performs additional tasks like database accessing, spell check etc �PHP 4(late 1998): runs on most popular web servers. Superglobals $_GET &$_POST was introduced 3 MCA/PDB 1/KPAL 7/30/2018
�Php 5: Object Oriented features �Php 6: native unicode support- never been released �Php 7: php next generation. More compact data structure 4 MCA/PDB 1/KPAL 7/30/2018
Structure and syntax of PHP �Universally used php structure: 5 <? php statements; ? > � Asp Style <%php statements; %> � HTML Script tag <script language=‘php’> statements</script> � SGML style <? Statements; ? > MCA/PDB 1/KPAL 7/30/2018
Integrating php with HTML <html> <head> <title>……. </title> </head> <body> <? php statement; ? > </body> </html> 6 MCA/PDB 1/KPAL 7/30/2018
PHP Comments �describe any line of code for understanding �also be used to hide any code. �PHP supports �single line �multi line comments. �shell style comments. 7 MCA/PDB 1/KPAL 7/30/2018
Single Line Comments �// (C++ style single line comment) �# (Unix Shell style single line comment) 8 MCA/PDB 1/KPAL 7/30/2018
Multi Line Comments � /* ………. */. <? php /* Anything placed within comment will not be displayed on the browser; */ echo "Welcome to PHP multi line comment"; ? > 9 MCA/PDB 1/KPAL 7/30/2018
Syntax �case sensitive � terminated by semicolons �Braces make blocks Example: if (3 == 2 + 1) print("Good - I haven't totally lost my mind. "); 10 MCA/PDB 1/KPAL 7/30/2018
PHP Variables �An identifier whose value is changed during the execution of the program-variable �is a name of memory location that holds data. �is a temporary storage that is used to store data temporarily. � declared using $ sign followed by variable name. �variable names are case sensitive �loosely typed language, automatically converts the variable to its correct data type. �No need for data type declaration 11 MCA/PDB 1/KPAL 7/30/2018
Naming Variable: Rules � must start with letter or underscore only. � can't be start with numbers and special symbols. �Second and successive characters may be letter or number �Only allowable special character is underscore(_) 12 MCA/PDB 1/KPAL 7/30/2018
�Syntax $variablename=value ; <? php $str="hello string"; $x=200; $y=44. 6; echo "string is: $str <br/>"; echo "integer is: $x <br/>"; echo "float is: $y <br/>"; ? > 13 MCA/PDB 1/KPAL OUTPUT: string is: hello string integer is: 200 float is: 44. 6 7/30/2018
Reference Variable($$) � $var - normal variable �The $$var - a reference variable that stores the value of the $variable inside it. 14 <? php $x="U. P"; $$x="Lucknow"; echo $x. " "; echo $$x. " "; echo "Capital of $x is ". $$x ; MCA/PDB 1/KPAL ? > OUTPUT: U. P Lucknow Capital of U. P is Lucknow 7/30/2018
Constants � constants are name or identifier that can't be changed during the execution of the script. �PHP constants can be defined by 2 ways: �Using define() function �Using const keyword �PHP constants follow the same PHP variable rules. �constants should be defined in uppercase letters. 15 MCA/PDB 1/KPAL 7/30/2018
constant: define() Case sensitive: true or false define(name, value, case-insensitive) ; 16 MCA/PDB 1/KPAL 7/30/2018
constant: const keyword � defines constants at compile time. � It is a language construct not a function. � bit faster than define(). � always case sensitive. <? php const MESSAGE="Hello const by Java. Tpoint PH P"; echo MESSAGE; ? > 17 MCA/PDB 1/KPAL 7/30/2018
Data Types �Scalar Types �Compound Types �Special Types 18 MCA/PDB 1/KPAL 7/30/2018
Data Types: Scalar Types �There are 4 scalar data types in PHP. �Boolean: can be either true or false. �Integer: equal to C’s long type. 32 bit signed integer. range between – 2, 147, 483, 648 to +2, 147, 483, 647 �Written in decimal, hexa decimal & octal � 0 X 9 A 56 � 0765(octal) �Float: equal to C’s double type �size is 8 bytes and it has a range of �approximately 2. 2 E– 308 to 1. 8 E+308. � 3. 14, +0. 9 e-2, -170000. 5, 54. 6 E 42 19 MCA/PDB 1/KPAL 7/30/2018
�String: �sequence of characters that are always internally nullterminated �use doublequotes ("), single quotes (') or here-docs to delimit them �Escape sequences: n, t, ”, \, �Example: "The result is $resultn“ �Single quote: 'Today's the day' 20 MCA/PDB 1/KPAL 7/30/2018
�Here-Docs : embed large pieces of text in scripts � may include lots of double quotes and single quotes, without having to constantly escape them. �Example: <<<THE_END PHP stands for "PHP: Hypertext Preprocessor". The acronym "PHP" is therefore, usually referred to as a recursive acronym THE_END 21 MCA/PDB 1/KPAL 7/30/2018
Data Types: Compound Types �There are 2 compound data types in PHP. �Object : are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class. �Array 22 MCA/PDB 1/KPAL 7/30/2018
Data Types: Special Types �There are 2 special data types in PHP. �Resource: represent a PHP extension resource such as a database query, an open file, a database connection, and lots of other external types. �NULL: only one possible value: the NULL value �Indicates empty value �Example: $a=NULL; 23 MCA/PDB 1/KPAL 7/30/2018
Display Statements �Echo: �is a language construct not a function �don't need to use parenthesis with it. �to use more than one parameters, it is required to use parenthesis. void echo ( string $arg 1 [, string $. . . ] ) 24 MCA/PDB 1/KPAL 7/30/2018
Example �printing escaping characters <? php echo "Hello escape "sequence" characters"; � printing variable value ? > <? php $msg="Hello Java. Tpoint PH P"; �printing multi line string echo "Message is: $msg"; ? > 25 MCA/PDB 1/KPAL 7/30/2018
Print �is a language construct �don't need to use parenthesis with the argument list. �Unlike echo, it always returns 1. �can be used to print string, multi line strings, escaping characters, variable, array etc. int print(string $arg) 26 MCA/PDB 1/KPAL 7/30/2018
Operators � Arithmetic Operators � Comparison Operators � Bitwise Operators � Logical Operators � String Operators � Incrementing/Decrementing Operators � Array Operators � Type Operators � Execution Operators � Error Control Operators � Assignment Operators 27 MCA/PDB 1/KPAL 7/30/2018
Arithmetic Operators �+, -, * , / , % 28 <html> <head> <title>Arithmetical Operators</title> </head> <body> <? php $a = 42; $b = 20; $c = $a + $b; echo "Addtion Operation Result: $c <br/>"; $c = $a - $b; echo "Substraction Operation Result: $c <br/>"; $<br/>"; MCA/PDB 1/KPAL ? > </body> 7/30/2018
- Slides: 28