Recursive Acronym for PHP: Hypertext Preprocessor
Total Page:16
File Type:pdf, Size:1020Kb
Introducation - PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. Features - i. It can communicate with database, collect form data, send, receive cookies. In addition to output html pages, it can output images, pdf, flesh movies, text, xml file. ii. It supports a wide range of databases thru db specific extension (mysql), ODBC, PDO. iii. It can support for talking to other services using protocols such as - LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows) and countless others. PHP has support for instance of Java objects and using them transparently as PHP objects. iv. Run on a wide range of os (win, linux, mac, …) and many servers like apache, IIS. v. Support both procedural and OOPs. History - PHP as it's known today is actually the successor to a product named PHP/FI. FI stands for form interprator. Created in 1994 by Rasmus Lerdorf to make his dynamic resume in c language. In Jun 1995, he released his source code to public for further development and improvement. Later it becomes PHP. It support database communication. Its latest version php 7.3 released on 6-12-18. It support Unicode character set. Installation – It can be freely downloaded from www.php.net. It is server side scripiting but can be used for command line scripting at system prompt (# in linux of dos prompt in windows). To run php three things are required – Php Parser (CGI server module), Web Server (apache, iis (only for windows) ), browser. It can be installed by utilities --- Xampp, Wamp, LAMP ( for linux only). Php IDE / editors – Codelobster, Ecilipse, netbeans, phpedit, zend studio,rapid php, komoda. IDEs can debug php code and can show the output of php script. It (.php file) can run on web browser, can be embeded in html using html command script. < ? in html --- <script language=”php”> …............... …............... ? > or </script> Printing in php – echo, print are the construct of php that can be used to print the content on web page. Both are same in functionality but with a little difference that print return a value 1 but echo does not return anything. HTML formating commands can be used in printing. syntax- echo ( string $arg1 [, string $... ] ) -------- the paranthesis is optional print ex. (i) echo “<u>String</u>” (ii) echo “text $var1 ”.“text $var2 ” -------------- for concatanation (iii) echo 'string' (iv) echo “string1 \n string2” --------- \n is only for command prompt (v) echo “string1 <br> string2” (vi) echo “string1”, “string2”, 1+2+3 (vii) echo <<<xyz This --------- here document is a book. xyz; Command line php – php runs at server side and show result on client's browser. It can run at comnmand promt. The command line version is called CLI (Command line interprater). At command line, the php document can be debugged. ex. – c:\apache\php> php abc.php $ php -v ----- version of php # php -h ----- php help control characters in php-- \n --- new line \r --- return key \t ---- tab \\ ---- backlesh \$ \” Comments in php code – /* ----------- ------ multiple line comment ----------- */ // ----- Single line or right part comment variable – A name that holds a value in memory. Php is case-sensative. It is a typeless language . Although it support several data types but you can store any value in a variable without type declaration. Php rules for variable nameing – (i) Must begin with $ (ii) Second character may be alphabet or underscore( _ ) (iii) remaining characters may be alphabet, digit, underscroe or its combination. A variable can be removed (undefined) by using command unset. unset(variable); Interpolating String – It is a shortcut way to put varaibles within quotations to print their values. ex. $a = computer $p = 50 echo “This is a ”, $a, “book and its price is Rs. ”, $p, “. you can buy it.” we can rewrite it using string interpolation as , echo “This is a $a book and its price is Rs. $p . you can buy it. Two books are of Rs. $p * 2” Note – only double quotation (“) is allowed in string interpolation. If you want to write (show) a string attached to value of variable. You can write within string as , ex. echo “The total of {$a}+{$b} is {$c}. echo “the value of $$a is $a .” A variable may hold another variable as its contents --- ex. $a=5; $b=”a”; echo $a, $$b, $b output → 5 5 a Constants – also called literals. Any data value that represent the same value does not (Can't) be changed during program execution , nor further in coding. Php allows naming constants. ex. define(“PI”, 3.14, TRUE) ------ PI is naming constant, it is not case sensative, means PI, pi, Pi, pI all are same define(“PI”, 3.14) ------- PI is a naming constant and it is case sensative. The value of constant can't be changed in further coding. In php, several naming constants are available. ex. _LINE_ _FILE_ _FUNCTION_ _CLASS_ _METHOD_ PHP_VERSION PHP_OS PHP internal data types – boolean, integer, float, string, array, object, resource, NULL ex. $a = 5 $b = 3 $c = $a / $b echo $c ------------------------------------------- 2 $c = (float) $a / $b echo $c ------------------------------------------- 1.666 operators in php Sn Operation Operators 1 Arithmetic operator * / + - modulas, exponentation % ** 2 Increment, Decrement ++ -- 3 Assignment Operator = 4 Arithmetic+ Assignemtn *= /= += -= %= 5 Comparison Operator == (equals) === (identical) != (not equal) !== (not identical) > >= < <= <=> …............................... Spaceship – new in php7 6 Logical Operator and && or || xor ! 7 String operators . .= 8 Bitwise operators & | << --- left shift means each step multiply by 2 >> --- right shift means each step divide by 2 9 Backticks operator <pre>dir</pre>, example – <?php $output = `ls -al`; echo "<pre>$output</pre>"; ?> 10 Array operator + --------- union == === != !== <> 11 Type operator Instanceof ----- object_name instanceof class_name returns true if object belongs to class, otherwise false Note - (i) ref. sr. no. 5, <=> ------------------- binary comparison operator, op-1 <=> op2, returns - 1 – when op1 < op2 0 – when both operands are equal 1 - when op1 > op2 (ii)ref. sr. no. 5, If you compare a number with a string then each string is converted to a number and the comparison takes place numerically. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value. Decision Making – Two multiline statements are used – if, switch. Multilines are enclosed within curly braces-- { }. If the statement has a single line then use of { } is optional..