Posts

Showing posts with the label PHP Notes

PHP Notes 8

PHPNotes 8  Lakshman Gupta 9:48 AM 10/8/2019 PHP Notes 8 Lakshman Gupta PHP Tutorial reset($user); // Rewind for the next foreach foreach ($fields as $field) { print " <th>$field</th>\n"; } print " </tr>\n"; $header_printed = true; } // end of print header. // Output data in the record retrieved. print "\t<tr>\n"; foreach ($user as $col_value) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } print "</table>\n"; mysql_free_result($qno); // Free resultset. mysql_close($dblink); // Close connection. ?> Communication with Other Databases Unlike other scripting languages for Web page development, PHP is open-source, cross-platform, and offers excellent connectivity to most of today's common databases including Oracle, Sybase, MySQL, ODBC (and others). PHP also offers integration with various external libraries which enable the developer ...

PHP Notes 7

PHP Notes 7 Lakshman Gupta 9:47 AM 10/8/2019 PHP Tutorial Regular Expressions Regular expressions, a powerful tool for manipulating text and data, are found in scripting languages, editors, programming environments, and specialized tools. Special characters: "/" "-" "^" "." "\" "^" "$" "*" "+" "(" ")" Equivalent Description or Meaning Examples /abc/ search for substring "abc" /[akt]/ search for one of "a", "k" or "t". /[^akt]/ true if string do not have any one of "a", "k" or "t". /[0-4]/ /[01234]/ search for one of "0", "1", "2", "3" or "4" /.../i Case insensitive * Zero or more /abck*pqr/ + One or more /abck+pqr/ \d [0-9] Any decimal digit [0123456789] \D Any character that is not a decimal digit \w [0-9A-Za-z_] Alphanumerics and underscore...

PHP Notes 6

PHP Notes 6 Lakshman Gupta  9:33 AM 10/8/2019 PHP Tutorial <html> .... _A_; .... ?> The following example displays all the cookies releated with the visited site: <?php if (!$_COOKIE) { $_COOKIE = $HTTP_COOKIE_VARS; } foreach ($_COOKIE as $ck => $val) { print "<dd>Cookie name: $ck; value: $val<br />\n"; } ?> Examples: · Webmail: store and use username. · Weather Site: store the selected city in a cookie, and next time the site is visited, the weather of the stored city is automatically displayed. Built-in Variables PHP has a number of built-in variables that give you access to your Web server's CGI environment, form/cookie data and PHP internals. Here are some of the most useful variables: PHP internal variables The $GLOBALS and $PHP_SELF variables shown in the table below are specific to PHP: Variable Name Description $GLOBALS An associative array of all global variables. This is the only variable in PHP that is av...

PHP Notes 5

PHP Notes 5 Lakshman Gupta 9:24 AM 10/8/2019 Calling the function: my_func(); Calling the function: $x = count_a ($name); Calling the function: salary_increase ($salary, 0.1); Calling the function: $xarr = get_values ($k, $m); // Here, $xarr becomes an array PHP Tutorial File System Functions: Open File $fp = fopen(“filename”, “mode”); Open a file, where mode is either “r”, “w” or “a” Close File fclose ($fp); Read a line from file $line = fgets($fp, 4096); Write a string to a file fwrite ($fp, $string); Read all the lines of a file $line = fgets($fp, 4096); while(!feof($fp)) { print "$line<br />\n"; $line = fgets($fp, 4096); } Reads line by line while NOT end-offile. Read all the lines of a file $lines = file ('filename'); foreach ($lines as $line) { print $line; } Reads all the lines of the file in an array; then print the lines. Delete a file unlink ('filename'); See http://www.php.net/manual/en/ref.filesystem.php for m...

PHP Notes 4

PHP Notes 4 Lakshman Gupta - 08/10/2009 PHP Tutorial Comparison Operators Example Name Result $a == $b Equal TRUE if $a is equal to $b. $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 Less than TRUE if $a is strictly less than $b. $a <= $b Less than or equal to TRUE if $a is less than or equal to $b. $a > $b Greater than TRUE if $a is strictly greater than $b. $a >= $b Greater than or equal to TRUE if $a is greater than or 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 identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only) Logical Operators Example Name Result $a && $b And TRUE if both $a and $b are TRUE. $a and $b And TRUE if both $a and $b are TRUE. $a || $b Or TRUE if either $a or $b is TRUE. $a or $b Or TRUE if either $a or $b is TRUE. ! $a Not TRUE if $a is not TRUE. $a xo...

PHP Notes 3

PHP Notes 3 Lakshman Gupta 9:16 AM 10/8/2019 keys values 24 "Ram" "Lakshman" "Pi" 3.14159 PHP Tutorial Conditionals and Looping Constructs PHP includes if and elseif conditionals, as well as while and for loops, all with syntax similar to C. The example below introduces these four constructs: // Conditionals if ($a) { print "a is true<br/>\n"; } else if ($b) { print "b is true<br/>\n"; } else { print "neither a or b is true<br/>\n"; } // Loops for ($j = 0; $j < 10; $j++) { print "j = $j<br />\n"; } while ($d) { print "ok<br />\n"; $d = test_something(); } do { $c = test_something(); } while ($c); Array Traverse Constructs: foreach ($array as $value) {...} // To traverse elements (values) in an array. { .... } foreach ($array as $key => $value) {...} // To traverse elements (key + value) in an associative array. { .... } Operat...

PHP Notes 2

PHP Notes 2 Lakshman Gupta 8:59 AM 10/8/2019 PHP Tutorial In the following example, four variables are automatically declared by assigning a value to them: <?php $number = 5; $string1 = "this is a string\n"; $string2 = 'this is another "string"'; $real = 37.2; ?> Strings The string constants may be enclosed between double-quotes ( " ) or single-quotes ( ' ). In a double-quote enclosed string, the variable conversion is done automatically. $city = "Ankara"; print "I live in $city."; $height = 182; print "I am $height cm tall."; String operators: Concatenation: . Concatenation to the end: .= $str1 = $lastname . ", " . $firstname; $str2 .= $str3; Arrays PHP supports two type of arrays: · Numerically indexed arrays · Associative arrays. Numerically indexed arrays: The indices in numerically indexed arrays starts from 0 by default, although this can be altered. The elements of ...

PHP Notes 1

What is PHP? 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. How PHP came into being PHP started as a quick Perl hack written by Rasmus Lerdorf in late 1994. Over the next two to three years, it  evolved into what we today know as PHP/FI 2.0. PHP/FI started to get a lot of users, but things didn't start flying until Zeev Suraski and Andi Gutmans suddenly came along with a new parser in the summer of 1997, leading to PHP 3.0. PHP 3.0 defined the syntax and semantics used in both versions 3 and 4. Why yet another language? People often ask "why invent yet another language; don't we have enough of them out there"? It is simply a matter of "the right tool for the right job". Many Web developers found that existing tools and languages were not ideal for the specific task of embedding code in mark...