tag:blogger.com,1999:blog-90808352007-04-15T16:24:40.988-07:00PHP Journalcloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comBlogger24125tag:blogger.com,1999:blog-9080835.post-1106145773123888862005-01-19T06:34:00.000-08:002005-01-19T06:46:03.736-08:00Take a Break, Solve a PuzzleIt is always good to relax after long hours of coding. You clear your mind, you think better, you feel better, and you WILL code better programs if you do not over-strain yourself. <br /> <br />Here's a good <strong>word search puzzle</strong> web site to visit ... <a href='http://www.webvroom.com'>http://www.webvroom.com</a> <br /> <br />Do remember to 'vroom' back to PHP after your break.cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100874268086856842004-11-19T06:19:00.000-08:002004-11-19T06:32:14.446-08:00PHP Form TrapPHP are <strong>great</strong> for processing input forms ... Just look at this form: <br /> <br />&#60;html&#62; <br />&#60;body&#62; <br />&#60;form action="process.php" method="post"&#62; <br />What is your name: &#60;input name="username" type="text"&#62;&#60;br&#62; <br />What is your age: &#60;input name="age" type="text"&#62;&#60;br&#62; <br />&#60;input name="Submit" type="submit" value="Submit Now"&#62; <br />&#60;/form&#62; <br />&#60;/body&#62; <br />&#60;/html&#62; <br /> <br />To get the submitted values, this code will suffice: <br /> <br />&#60;?php <br /> <br />echo("The user's name is " . $username); <br />echo("The user's age is " . $age); <br /> <br />?&#62; <br /> <br />( Simply cool! ... You might be thinking already...) <br /> <br />Now go on ... Run the code and see what you get ... <br /> <br />Do you get this result? <br /> <br />The user's name is John. (Assuming you entered Jack) <br />The user's age is 23. (Assuming you entered 23) <br /> <br />No? Do you get an <strong>error</strong>? <br /> <br />( Yep. What am I doing wrong? ... Are you wondering? ) <br /> <br />Let's see ... if you encounter the following error, you are not alone ... <br /> <br /><strong><em>Notice: Undefined variable: username in ......\test.php on line 3 <br />Username is <br />Notice: Undefined variable: age in ......\test.php on line 4 <br />Age is </em></strong> <br /> <br />Almost every PHP newbie suffers from this maddening experience. A simple form exercise turns out to be minutes (hopefully not hours ...) of debugging. <br /> <br />Luckily for you, you won't suffer the same trauma. <br /> <br />The reason why this error occur lies in the <strong>"php.ini"</strong> file. <br /> <br />Look for <strong>register_globals = off</strong>. Turn it <strong>on</strong> and try again. <br /> <br />You should be popping champagne this time! Well let's not be estatic. <br />Although it works for now, it might be better to <strong>turn it off</strong> again. <br /> <br />(Any reason why?) <br /> <br /><strong>Security issue</strong>. <br /> <br />If someone create a bogus form with tonnes of input fields and send it to your php script, your script will end up with tonnes of landmines in the global register. <br /> <br />Sounds bad enough? <br /> <br />Remember to turn it off. <br /> <br /> <br /> <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100694890291307492004-11-17T04:33:00.000-08:002004-11-17T04:34:50.290-08:00Function Calls at RuntimeIt is possible to <strong>select the correct function</strong> to call at run-time ... here's how: <br /> <br /><?php <br /> <br />function requestLogin() { <br /> <br />print("Please login first.<br>"); <br /> <br />} <br /> <br />function welcomeUser() { <br /> <br />print("Welcome to my homepage.<br>"); <br /> <br />} <br /> <br />function checkUser($userLogin) { <br /> <br />global $runFunction; <br /> <br />if ($userLogin) { <br /> <br />$runFunction = "welcomeUser"; <br /> <br />} else { <br /> <br />$runFunction = "requestLogin"; <br /> <br />} <br /> <br />$runFunction(); <br /> <br />} <br /> <br />$userLogin = true; <br /> <br />checkUser($userLogin); <br /> <br />$userLogin = false; <br /> <br />checkUser($userLogin); <br /> <br />?> <br /> <br />prints: <br /> <br />Welcome to my homepage. <br />Please login first.cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100613047869385642004-11-16T05:49:00.000-08:002004-11-17T04:40:04.403-08:00PHP Array FunctionsRefer to the PHP manual for more details. <br /> <br /><strong>is_array($array)</strong> - Tests if $array is an array. Returns (true|false). <br /><strong>array_merge()</strong> - Combines 2 or more arrays. Returns combined array. (From PHP4 onwards) <br /><strong>array_push()</strong> - "Pushes" 1 or more items into an array. <br /><strong>array_shift()</strong> - "Pops" 1 item from an array. Returns item. <br /><strong>array_slice()</strong> - Duplicates portions of another array. Returns new array. <br /><strong>sort()</strong> - Sorts an array (either alphabetically or numerically depending on item datatype). Returns nothing. <br /> <br />&#60;?php <br /> <br />// Start with an array <br />$cookbook1[] = "chicken pie"; <br /> <br />// add another recipe <br />array_push($cookbook1, "durian cake"); <br /> <br />// show and remove the first recipe from cookbook <br />print(array_shift($cookbook1) . "<br>"); <br /> <br />// create another array <br />$cookbook2 = array ("apple pie", "orange pie", "milkshake"); <br /> <br />// merge both cookbooks <br />$cookbook3 = array_merge($cookbook1, $cookbook2); <br /> <br />// sort the cookbook <br />sort($cookbook3); <br /> <br />// print the cookbook <br />foreach ($cookbook3 as $recipe) { <br /> print("$recipe . <br>"); <br />} <br /> <br />?&#62; <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100403145858887782004-11-13T19:31:00.000-08:002004-11-13T19:32:25.856-08:00Variable within Another VariableA quick quiz ... <br /> <br />Is this code legal? ... <br /> <br />&#60;?php <br /> <br />$fruit = "apple"; <br />$temp = "fruit"; <br /> <br />print($$temp); <br /> <br />?&#62; <br /> <br />If you answered "Yes", congrats ... you are right. If you said "No", better luck next time! <br /> <br />Read on! <br /> <br />$$temp actually evaluates to $fruit. Hence, the code is legal and the word "apple" is printed. <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100402998569899462004-11-13T19:29:00.000-08:002004-11-16T05:41:43.113-08:00PHP if ... elseif ... else ...Sample code: <br /> <br />&#60;?php <br /> <br />$temperature = 0; // degrees celsius <br /> <br />if ($temperature == 0) { <br /> print("I am freezing."); <br />} elseif ($temperature == 100) { <br /> print("I am boiling."); <br />} else { <br /> print("In between boiling and freezing points."); <br />} <br /> <br />?&#62; <br /> <br />prints <br /> <br />I am freezing. <br /> <br />Shortcut using the <strong>ternary operator</strong>: <br /> <br />If there are only two possible alternatives, you can use the ternary operator as a shortcut: <br /> <br />eg. <br /> <br />&#60;?php <br /> <br />$reply = 1; <br /> <br />if ($reply == 1) { <br /> $result = "yes"; <br />} else { <br /> $result = "no"; <br />} <br /> <br />print($result); <br /> <br />?&#62; <br /> <br />is the same as : <br /> <br />&#60;?php <br /> <br />$reply = 1; <br />$result = (($reply == 1)? "yes" : "no"); <br />print($result); <br /> <br />?&#62; <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100402950460563732004-11-13T19:27:00.000-08:002004-11-17T04:29:45.513-08:00"Pass By Value" versus "Pass By Reference"PHP supports <strong>"pass by reference"</strong> variables by prefixing a variable with an ampersand(&)(PHP4 onwards ...). By default, variables are <strong>"passed by value"</strong>. <br /> <br />e.g. <br /> <br />&#60;?php <br /> <br />$varA = "apple"; <br />print($varA . "<br>"); <br /> <br />$varB = "orange"; <br />print($varB . "<br>"); <br /> <br />$varC = "pineapple"; <br />print($varC . "<br>"); <br /> <br />$varC = &$varA; // reference $varA <br />print($varC . "<br>"); <br /> <br />$varA = "watermelon"; <br />print($varC . "<br>"); <br /> <br />$varC = &$varB; // reference $varB <br />print($varC . "<br>"); <br /> <br />$varC = $varA; // copy of $varA's value <br />print($varC . "<br>"); <br /> <br />?&#62; <br /> <br />Prints results: <br /> <br />apple <br />orange <br />pineapple <br />apple <br />watermelon <br />orange <br />watermelon <br /> <br />To pass a <strong>variable by reference to a function</strong>, do this: <br /> <br />&#60;?php <br /> <br />function passToFunction1(&$arg1) { <br /> <br />// ... <br /> <br />} <br /> <br />passToFunction1($varByRef); <br />?&#62;cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100402692157940092004-11-13T19:22:00.000-08:002004-11-13T19:24:52.156-08:00Looping in PHPThere are <strong>2</strong> standard types of <strong>while loops</strong>. <br /> <br /><strong>Type 1: while loop</strong> <br /> <br />eg. <br /> <br />&#60;?php <br /> <br />$x = 0; <br />while ($x &#60; 10) { <br /> // do something <br /> $x++; <br />} <br /> <br />?&#62; <br /> <br />The conditional test is evaluated <strong>BEFORE</strong> the code block is executed. <br /> <br /><strong>Type 2: do ... while loop</strong> <br /> <br />eg. <br /> <br />&#60;?php <br /> <br />$x = 0; <br />do { <br /> // do something <br /> $x++; <br />} while ($x &#60; 10); <br /> <br />?&#62; <br /> <br />The conditional test is evaluated <strong>AFTER</strong> the code block is executed. <br />This means that the code block is run a minimum of 1 time. <br /> <br />break and continue statements can affect looping if added in the code block. <br /> <br /><strong>break;</strong> abandons the while loop, and continues to the next statement. <br /><strong>continue;</strong> skips the current loop, but continues with the next loop. <br /> <br />eg. <br /> <br />&#60;?php <br /> <br />$x = 0; <br />while ($x &#60; 10) { <br /> if ($x == 5) { <br /> $x++; <br /> continue; <br /> } <br /> if ($x == 8) { <br /> break; <br /> } <br /> print($x . "&#60;br&#62;"); <br /> $x++; <br />} <br /> <br />?&#62; <br /> <br />prints: <br /> <br />0 <br />1 <br />2 <br />3 <br />4 <br />6 <br />7 <br /> <br />5 is missing as that iteration is skipped by continue; <br />8,9 are missing as break; terminated the loop when $x equals 8. <br /> <br /><strong>for loop statement:</strong> <br /> <br />eg. <br /> <br />&#60;?php <br /> <br />for ($x=0; $x&#60;10; $x++) { <br /> if ($x == 5) { <br /> continue; <br /> } <br /> if ($x == 8) { <br /> break; <br /> } <br /> print($x . "&#60;br&#62;"); <br /> <br />} <br /> <br />?&#62; <br /> <br />prints the same results as above.cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100402529064379342004-11-13T19:21:00.000-08:002004-11-16T05:43:53.660-08:00PHP SwitchSample code: <br /> <br />&#60;?php <br /> <br />switch ($temperature) { <br /> <br /> <br /> case 0: print("I am freezing."); <br /> break; <br /> <br /> case 100: print("I am boiling."); <br /> break; <br /> <br /> default: print("In between boiling and freezing points."); <br /> <br />} <br /> <br />?&#62; <br /> <br />Note the "break" statement after every case statement. If omitted, "fall-through" will occur.cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100402178453200482004-11-13T19:15:00.000-08:002004-11-13T19:16:18.453-08:00PHP ConstantsHow to <strong>define a PHP constant</strong>? <br /> <br />Use the function: <strong>define("CONSTANT_IN_UPPERCASE", value);</strong> <br /> <br />eg. <br /> <br />&#60;?php <br /> <br />define("CAR_WEIGHT", 100); <br /> <br />print("This car weighs ".CAR_WEIGHT." kg."); <br /> <br />?&#62; <br /> <br />displays <br /> <br />This car weighs 100 kg. <br /> <br />Note the missing $ sign before the constant name.cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100402054329069172004-11-13T19:13:00.000-08:002004-11-13T19:14:14.330-08:00PHP Tips1. Choose <strong>descriptive but concise</strong> variable names. <br />2. Use <strong>parentheses</strong> to clarify operations. <br />3. Always end the statement with a <strong>semicolon</strong>; <br />4. <strong>CONSTANTS</strong> names should be in <strong>UPPER-CASE</strong> by convention. <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100401897852732362004-11-13T19:07:00.000-08:002004-11-13T19:11:37.853-08:00Fun with OperationsI hope you have used a calculator before ... If not, go buy one and work on it ... it makes the following explanation much clearer ... <br /> <br /><strong>Maths Operation</strong> <br /> <br />Add: + <br />Subtract: - <br />Divide: / <br />Multiply: * (not 'x' as on a calculator;) <br />Modulus: % <br />Assignment: = <br /> <br /><strong>String Operation</strong> <br /> <br />Concatenate: . (yes ... it is a 'dot') <br /> <br /><strong>Shortforms</strong> <br /> <br />Add and Assign: += <br />Subtract and Assign: -= <br />Divide and Assign: /= <br />Multiply and Assign: *= <br />Modulus and Assign: %= <br />Concatenate and Assign: .= <br /> <br /><strong>Comparison Operation</strong> <br /> <br />Equal: == <br />Not equal: != <br />Equal and same datatype: === <br />Greater: &#62; <br />Greater than or equal: &#62;= <br />Less than: &#60; <br />Less than or equal: &#60;= <br /> <br /><strong>Logical Operations</strong> <br /> <br />OR: || <br />AND: && <br />NOT: ! <br />XOR: xor (... ONLY IF either left or right operand is true.) <br /> <br /><strong>Pre-Increment</strong>: ++$var <br /><strong>Pre-Decrement</strong>: --$var <br /><strong>Post-Increment</strong>: $var++ <br /><strong>Post-Decrement</strong>: $var-- <br /> <br />eg. <br /> <br />&#60;?php <br /> <br />$left = true; <br />$right = false; <br /> <br />$result = ($left xor $right); <br /> <br />print($result); <br /> <br />?&#62; <br /> <br />What will be the result? Try it out! (Result is "1".)cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100401555168104292004-11-13T19:05:00.000-08:002004-11-13T19:05:55.166-08:00Include PHP code in HTML PHP code can be added to any part of a HTML document. <br /> <br />For <strong>ease of maintenance</strong>, you can do the following: <br /> <br />&#60;!-- start of html file --&#62; <br /> <br />&#60;?php <br /> <br />// Put in as much PHP code at the top of the document as possible. <br />$title = "This is the title of the webpage"; <br />$body = "This is the body of the webpage"; <br /> <br />// and more code ... <br /> <br />?&#62; <br /> <br />&#60;html&#62; <br />&#60;head&#62; <br />&#60;title&#62;&#60;?php print($title); ?&#62;&#60;/title&#62; <br />&#60;/head&#62; <br />&#60;body&#62; <br /> <br />&#60;?php print($body); ?&#62; <br /> <br />&#60;/body&#62; <br />&#60;/html&#62; <br /> <br />&#60;!-- end of html file --&#62; <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100401442756115132004-11-13T19:03:00.000-08:002004-11-17T04:25:00.043-08:00PHP Associative ArraysHow to <strong>create an associative array</strong> ... <br /> <br />&#60;?php <br /> <br />$car = array ("weight"=&#62;"100kg", "year"=&#62;"2004", "price"=&#62;"7000"); <br /> <br />?&#62; <br /> <br />Alternative code: <br /> <br />&#60;?php <br /> <br />$car["weight"] = "100kg"; <br />$car["year"] = "2004"; <br />$car["price"] = "7000"; <br />$car["discount rebate"] = "12"; <br /> <br />?&#62; <br /> <br />To display the items in an array, do this: <br /> <br />&#60;?php <br /> <br />// display car properties <br />print($car["price"]."&#60;br&#62;"); <br /> <br />// display all car properties <br />foreach ($car as $property=&#62;$value) { <br /> print($property . " is " . $value . "&#60;br&#62;"); <br />} <br /> <br />?&#62; <br /> <br />How to <strong>sort an associative array</strong> ... <br /> <br />asort() - Sorts an associative array by value. Returns nothing. <br />ksort() - Sorts an associative array by key. Returns nothing. <br /> <br />eg. <br /> <br />&#60;?php <br /> <br />$fruittrolley = array ("apple"=&#62;"100", "orange"=&#62;"20", "pear"=&#62;"30"); <br /> <br />asort($fruittrolley); <br /> <br />print("After asort: &#60;br&#62;"); <br /> <br />foreach ($fruittrolley as $fruit=&#62;$no) { <br /> print("There are $no ${fruit}s.&#60;br&#62;"); <br />} <br /> <br />ksort($fruittrolley); <br /> <br />print("After ksort: &#60;br&#62;"); <br /> <br />foreach ($fruittrolley as $fruit=&#62;$no) { <br /> print("There are $no ${fruit}s.&#60;br&#62;"); <br />} <br /> <br />?&#62; <br /> <br />prints: <br /> <br />After asort: <br />There are 20 oranges. <br />There are 30 pears. <br />There are 100 apples. <br />After ksort: <br />There are 100 apples. <br />There are 20 oranges. <br />There are 30 pears. <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100401369123096692004-11-13T19:01:00.000-08:002004-11-13T19:02:49.123-08:00PHP ArraysHow to <strong>create an array </strong>... <br /> <br />&#60;?php <br /> <br />$fruitbasket = array ("Apple", "Orange", "Pear", "Mango", "Strawberry"); <br />$fruitbasket[] = "Rambutan"; // add a fruit to the list <br /> <br />?&#62; <br /> <br />Alternatively, you can use the following code: <br /> <br />&#60;?php <br /> <br />$fruitbasket[] = "Apple"; <br />$fruitbasket[] = "Orange"; <br />$fruitbasket[] = "Pear"; <br />$fruitbasket[] = "Mango"; <br />$fruitbasket[] = "Strawberry"; <br />$fruitbasket[] = "Rambutan"; <br /> <br />?&#62; <br /> <br />Note: Use <strong>count($arrayName)</strong> to count the number of items in the array. <br /> <br />To display the items in an array, do this: <br /> <br />&#60;?php <br /> <br />// display fruits in fruitbasket <br />for($i=0;$i&#60;count($fruitbasket);$i++) { <br /> print($fruitbasket[$i]."&#60;br&#62;"); <br />} <br /> <br />?&#62; <br /> <br />Alternatively, you can use the following code: <br /> <br />&#60;?php <br /> <br />// display fruits in fruitbasket <br />foreach ($fruitbasket as $item) { <br /> print($item."&#60;br&#62;"); <br />} <br /> <br />?&#62; <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100184369073319262004-11-11T06:45:00.000-08:002004-11-12T04:35:29.886-08:00Punctuate your PHPAlways end your PHP statement with a <strong>semicolon</strong> (;) <br /> <br />Now, what happens if you write bad grammar ... <br /> <br />PHP parser replies with an typical <strong>"unexpected T_PRINT"</strong> error message: <br /> <br /><em>Parse error: parse error, unexpected T_PRINT in xxxx\yyyyy\zzzzz\testfile.php on line 4 <br /></em> <br />So remember your semicolons;) even if it is the only statement in the block. <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100184222718129912004-11-11T06:41:00.001-08:002004-11-12T05:29:04.693-08:00HelloWorld in PHPSince this is a standard program in any language ... here's the PHP version ... <br /> <br />&#60;?php <br /> <br />print("Hello World"); <br /> <br />?&#62; <br /> <br />If you compare this with the JSP version of "Hello World" below, you will see why <strong>PHP is so MUCH MORE convenient to code</strong>. <br /> <br />import java.io.*; <br />import javax.servlet.*; <br />import javax.servlet.http.*; <br /> <br />public class HelloWorld extends HttpServlet { <br />&nbsp;&nbsp;public void doGet(HttpServletRequest req, HttpServletResponse res) <br />&nbsp;&nbsp;&nbsp;&nbsp;throws ServletException, IOException { <br />&nbsp;&nbsp;&nbsp;&nbsp;res.getWriter().println("Hello World"); <br />&nbsp;&nbsp;} <br />}cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100184315813139742004-11-11T06:41:00.000-08:002004-11-11T06:45:15.813-08:00PHP Tag StylesThere are <strong>4 ways</strong> of specifying PHP code: <br /> <br />1. The <strong>usual way</strong> ... <br /> <br />&#60;?php <br />// ... PHP code goes here <br />?&#62; <br /> <br />2. The <strong>shortcut</strong> ... <br /> <br />&#60;? /* PHP code goes here ... Beware if you have XML code in same page */ ?&#62; <br /> <br />3. The <strong>long-winded way</strong> ... <br /> <br />&#60;script language="php"&#62; <br /> <br />// ... PHP code goes here <br /> <br />&#60;/script&#62; <br /> <br />4. And finally, the <strong>ASP way</strong>. <br /> <br />&#60;% <br /> <br />// ... PHP code goes here <br /> <br />%&#62; <br /> <br />To discourage you from using the ASP way, asp tags are de-activated by default. You need to change the php.ini file to enable it. Look for asp_tags=off. Turn it on. <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100184073770619162004-11-11T06:39:00.000-08:002004-11-11T06:41:13.770-08:00Best PHP EditorsWhich is your <strong>favorite</strong> PHP editor? <br /> <br />1. Dreamweaver MX <br />2. TextPad <br />3. Notepad <br />4. Simple Text <br />5. BBEdit <br />6. HTMLKit <br />7. Emacs <br />8. PHP Studio <br /> <br />My vote goes to TextPad. It is simple. It is fast! It is really good! <br /> <br />Cast your vote now! <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100183322100620332004-11-11T06:27:00.000-08:002004-11-11T06:28:42.100-08:00PHP: The evolutionRasmus Lerdorf created the <strong>Personal Home Page Tools</strong> in 1994. (PHP) <br /> <br />A group of developers created a new parser and improved on PHP. (<strong>PHP3</strong>) <br /> <br /><strong>PHP4</strong> was released next, with the Zend engine at its core. <br /> <br /><strong>PHP5</strong> should be out by now ... <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100183208406413662004-11-11T06:24:00.000-08:002004-11-11T06:26:48.406-08:00Why I chose PHP1. PHP is MUCH <strong>easier</strong> to learn compared to other web languages. <br /> <br />2. It takes much <strong>fewer lines of code</strong> to produce the same results (i.e. Try coding a "HelloWorld" program in JSP, ASP and <br />PHP and you see what I mean). <br /> <br />3. It is <strong>free</strong>. So is MySQL, the database. Ah! Not forgetting Apache, the web server. And Linux! Your wallet should be happy! <br /> <br />4. Powerful. PHP has <strong>many useful functions</strong> ready for use. There is no need to re-write the code to access the database etc. <br /> <br />5. PHP runs on <strong>Windows, Linux, Unix</strong> etc. <br /> <br />6.<strong> Open-source</strong> and mainstream ... it is easier to find help if you run into problems. <br />cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100009074134581342004-11-09T05:59:00.000-08:002004-11-17T04:16:50.860-08:00PHP Variables and DatatypesWhat is a variable? (You are kidding ... aren't you?) <br /> <br /><strong>How to set a variable in PHP?</strong> <br /> <br />This is how you do it ... <br /> <br />$variableName = value; <br /> <br />Letters, numbers, underscore (_) are <strong>allowed</strong> in variable names. <br />Spaces ( ), non-alphanumeric characters are <strong>NOT allowed</strong> in variable names. <br /> <br />Variable names <strong>CANNOT</strong> start with a number! <br /> <br />Here are the <strong>basic datatypes </strong>in PHP. The value of a variable can be one of these datatypes: <br /> <br />1. Integer eg. 12345 <br /> <br />2. Double eg. 123.45 <br /> <br />3. String eg. 'string' or "string" <br /> <br />4. Boolean either true or false (PHP4 onwards) <br /> <br />Note that <strong>PHP does not impose strong type checking</strong>. (i.e. A variable may take on different datatypes at different points of execution.) <br /> <br />&#60;?php <br /> <br />// Defines an integer <br />$noOfPeople = 12345; <br /> <br />// Defines an double <br />$priceOfProduct = 123.45; <br /> <br />// Defines a boolean <br />$userHasRegistered = true; <br /> <br />// Defines a string <br />$welcomeMessage = "Hello World"; <br /> <br />?&#62; <br /> <br />(... You mean you really don't know what a variable is? ... hmmm ... well ... treat it as a box containing a value which may vary. )cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100006039611817572004-11-09T05:11:00.000-08:002004-11-09T05:54:40.996-08:00PHP VersionAll the code snippets in this blog have been tested with<strong> PHP Version 4.3.8</strong>. <br /> <br />Now how do you know which version you are running? <br /> <br />Simple ... <br /> <br />Run this line in your code: <br /> <br />&#60;?php <br /> <br />phpinfo(); <br /> <br />?&#62;cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.comtag:blogger.com,1999:blog-9080835.post-1100004667591333092004-11-09T04:44:00.000-08:002004-11-12T05:59:32.693-08:00How to comment in PHPThere are <strong>3 ways of commenting in PHP</strong>: <br /> <br /><?php <br /> <br /># This line is commented out (single line comment). <br /> <br />... <br />// so is this line (another single line comment). <br /> <br />... <br /> <br />/* <br /> <br />Is this line a comment? Make a guess! ( ... Alright, it is a multi-line comment.) <br /> <br />*/ <br /> <br />?>cloudninehttp://www.blogger.com/profile/10621457486318098750noreply@blogger.com