PHP

September 21, 2007 RENU

  1. It is the only open-source(free, users can view source code, alter & redistribute) server side scripting lang.
  2. It is typically used in combination with web server like Apache.
  3. Requests for PHP scripts r received by apache web server & r handled by PHP interpreter. Rslts obtained after execution r returned to web server which transmits them to client browser.
  4. Every PHP st ends in a semi colon. Not needed for just one or for last sentense.
  5. Blank lines, e’thing outside tags is ignored & returned as is. Only code betn tags read & executed.
  6. Variable name is preceded by dollar symbol ($) & must begin with letter &/or underscore. They r case sensitive. To assign value to variable, =symbol used. exs: $age = $dob + 15; or $angle1 = $angle2 = $angle3 = 60; PHP supports no of variable types like integers, arrays, strings(enclosed in double or single quotes), floating point nos(fractional nos with decimals).
  7. echo () function along with print () function used to print data to output device i.e browser
  8. Supports many data types like simple numeric, character, string, boolean types, more complex arrays & obj’s. exs; $auth = true; $age = 99; $temperature = 56.89;
  9. Form is for interaction with user where user has to fill info needed. “Action” attribute specifies name of server side script(name.php) that will process info entered into form. “Method: attribute specifies how info will be passed. When u enter info in form.htm & submit it, name.php mentioned in that form reads & displays it to u. It can see variables & their values through PHP variable $ _POST. U can then access value of form variable using “name” inside $ _POST container.
  10. comparison operators: , , ==, ===, !=, = for less than, greater than, not equal to, equal in value, equal in type & value, not equal to, less than or equal to, greater than or equal to. Rslt for comparison is always Boolean: true(1) or false(doesn’t print anything).
  11. Logical operators: AND(for all conditions true), OR(any condition true), NOT(if condition false), XOR(if either of 2conditions true & false if both r true), returns true.
  12. Instead of creating 2files, .htm & .php, u can combine both pages into one with conditional st’s. For this, assign a name to form submit ctrl, then chk whether special $_POST container variable contains that name when script 1st loads up. If it does, form has already been submitted & u can process data. $_SERVER array is a special PHP variable which always holds server info, including path & name of currently executing script.
  13. Loop ctrl lets u repeat same set of php st’s/cmd’s over & over again. A. while (condition is true) { do this!} B. do {do this!} while (condition is true). St’s within loop executed 1st & condition to be tested is chk’d afterwards i.e st’s within curly braces would be executed at least once. But they both loop till condition is true. To iterate specified no of times, use for loop. C. for (initial value of counter; condition; new value of counter) {do this!}. Counter valuable is initialized to numeric value & keeps track of no of times loop executed. Before each execution of loop, “condition” is tested. If it evaluates to true, loop will execute once more & counter will be incremented; if false, loop will be broken & lines following it will be executed. D. foreach() loop runs once for each array element passed to it as argument, moving forward through array on each iteration. It does not need counter, so easy than for()loop. Works with ass arrays too.
  14. Increment operator(++) automatically increments variable value it is attached to by 1.
  15. Array variable(Here pizza toppings) lets u store multiple values in a single variable. ex: $fruits= array(‘apple’, ‘banana’, ‘cherry’); print_r($fruits); print _r() func lets u peek inside array while debugging. Array elements r accessed via index no, 1st element starting at zero. So $fruits[0] to access ‘apple’. U can also define array as $fruits[0] = ‘apple’; $fruits[1] = ‘banana’; ….; Hash/associate array: Lets u incl user defined keys. => associates key & its value. ex: $fruits = array(‘red’ => ‘apple’, ‘yellow’ => ‘banana’); print_r($fruits); U can also define as $menu[‘b.fast’] = ‘o.lette’; $menu[‘lunch’] = ‘beef’; array_push() lets u add an element to end of existing array. ex: $pasta = array(‘penne’, ‘m.roni’); array_push($pasta, ‘s.ghetti’); print_r($pasta); array_pop() removes end element. ex: $pasta = array(‘penne’, ‘m.roni’); array_pop($pasta); print_r($pasta); array_shift() pops element off top of array. array_unshift() adds elements to beginning of array. It along with array_push() doesn’t work with ass arrays, so to add new values then, use $arr[$key] = $value explode() splits string into sm components based on user defined delimiter, & returns pcs as elements as array. ex: $str = ‘red, blue, green’; $colors = explode(‘,’, $str); print_r($colors); Opp is implode(). Sort(), rsort() to sort alphabetically/numerically in asc or desc order Sizeof() returns size of no of elements within array. Mostly used in loops counters to ensure loop iterates as many times there r elements in array. array_keys & array_values in ass array used to get list of all keys & values within array.
  16. To read a file or to write to file, u need to open file, obtain file handle, use file handle to do the job, then close file. fopen() creates file handle, $fh, which php needs to read data from file. It accepts 2arg’ts: name & path to the file, then string indicating ‘mode” in which file is to be opened. 3modes, 1. ‘r’- opens file in read mode. 2. ‘w’- write mode destroying existing file contents. 3. ‘a’- append mode, preserving existing file contents. file_put_contents()takes a string,writes it to file in single line of code fread() reads file & puts contents into variable. fclose() to close file die() if php cant read file due to error, terminates script & displays user specified error msg coolfile() reads entire file into array with 1line of code using loop. file() opens file, reads it into array, closes file. file_get_contents() reads entire file into string.
    is_dir() returns Boolean, indicating if specified path is a direc
    is_file() returns Boolean, indicating if specified path is a regular file
    is_link() returns Boolean indicating if specified file is a symbolic link
    is_executable() returns Boolean indicating if specified file is executable
    is_readable() returns Boolean indicating if specified file is readable
    is_writable()- returns Boolean indicating if specified file is writable require_once() & include_once()- ensure that file which has already been read is not read again for performance reasons etc. require() returns fatal error if named file cant be found & halts script processing, while include() returns warning, but allows script processing to continue.
    filesize() – gets file size in bytes.
    filemtime() – gets file’s last modification time
    filamtime() – gets file’s last access time
    fileowner() – gets file owner
    filegroup() – gets file group
    fileperms() – gets file permissions
    filetype() – gets file type
  17. Conditional st:
  • If() construct- if (condition) { do this! } . If st evaluates to true, PHP code within curly braces executed or else skipped & following lines executed.
  • if-else construct- if (condition) {do this!} else {do this!}. U can even nest. if ($x == ‘y’) { if ($z = ‘777’) { if ($w == ‘uv’) {$a = ‘…’; } } } or in easy way if ($x == ‘y’ && $z = ‘777’ && $w == ‘uv’) {$a = ‘…’;}
    if (first condition is true) { do this! } elseif (second condition is true) { do this! } elseif (third condition is true) { do this! } … and so on … else { do this! }
  • switch (decision-variable) { case 1st condition is true: do this! case 2nd condition is true: do this! … and so on…}. break keyword is used to break out of switch() st block & move imm’ly to following lines. default k.word is used to execute default set of st’s when variable passed to switch() doesn’t satisfy any conditions listed within block.
  1. If() construct- if (condition) { do this! } . If st evaluates to true, PHP code within curly braces executed or else skipped & following lines executed.
  2. if-else construct- if (condition) {do this!} else {do this!}. U can even nest. if ($x == ‘y’) { if ($z = ‘777’) { if ($w == ‘uv’) {$a = ‘…’; } } } or in easy way if ($x == ‘y’ && $z = ‘777’ && $w == ‘uv’) {$a = ‘…’;}
  3. if (first condition is true) { do this! } elseif (second condition is true) { do this! } elseif (third condition is true) { do this! } … and so on … else { do this! }
  4. switch (decision-variable) { case 1st condition is true: do this! case 2nd condition is true: do this! … and so on…}. break keyword is used to break out of switch() st block & move imm’ly to following lines. default k.word is used to execute default set of st’s when variable passed to switch() doesn’t satisfy any conditions listed within block.
Advertisements

Entry Filed under: Computer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to comments via RSS Feed

Pages

Categories

Calendar

September 2007
M T W T F S S
« Aug   Oct »
 12
3456789
10111213141516
17181920212223
24252627282930

Most Recent Posts

 
%d bloggers like this: