Skip to main content

Posts

Showing posts from January, 2012

PHP User Defined Functions

           PHP is packed full with many useful built-in functions, such as print() and mysql_query(), which meet a  variety of needs. However, there will be times when a non-standard function would best meet the needs of  the application that is being built.  This is where user defined functions come in handy.  The programmer  can of course write the same piece of code over and over again in their applications to produce the result or  they can create a user defined function that can be reused over and over again.  This is the primary  advantage of custom functions.  The actual code associated with built-in functions  consists of the keyword function followed by the  function name.  Next comes the parentheses, which may contain variable names, and the actual function  code itself, enclosed in curly braces:  function function_name () {      -- Statements that make up the function code  }  The following examples demonstrate some of the uses and capabilities for creating a user d

PHP Break and Continue

              With iterative control statements, PHP provides two means of manually interrupting the flow of the loops. These two commands include break (which has already been used in examples up to this point) and  continue.  The following example demonstrates how these two loop interrupts can be used in iterative control statements: <?php     for ($test=1; $test < 10; $test = $test + 1) {         if ($test==3) continue;         if ($test==6) break;         echo "The test number is $test<br>";     } ?> The following output would be displayed when this PHP script was run: The test number is 1 The test number is 2 The test number is 4 The test number is 5               When the  $test variable was equal to 3, the script skipped the remain code and performed the next  iteration of the for function.  This resulted in the number 3 not being printed. In addition, when the $test  variable was equal to 6, the break function ended the for function

PHP FOR STATEMENT

FOR The for function is the more complex sibling of the while function and provides a more streamline and  complex looping mechanism.  The for function takes three expressions; the first expression is evaluated  by default at the first iteration of the loop, the second expression is evaluated at the beginning of each  iteration (and determines if the loop will continue) and the third expression is evaluated at the conclusion of  each loop.  Any of the expressions can be empty and the logic that would take place in the expression can  be substituted in the body of the function itself.  The statement list within the for code body can consist of  one or more statements.  for (expression1; expression2; expression3)  {      -- statements that execute while the expressions evaluates false  }  The following examples demonstrate how the for iterative control statement can work:  <?php      for ($ctemp = 0; $ctemp <= 20; $ctemp = $ctemp + 1) {          $ftemp = 32 + $ctem

PHP Control Flow (while)

While             The  while function is the simplest of all the iterative control statements.  The statement list within a while statement is repeated as long as the condition evaluated has not been met.  The statement list within the while code body can consist of one or more statements. while (expression)  {     -- statements that execute while the expression evaluates false } It is a best practice to build error detecting logic into any iterative control statement.  This error control logic should be built in such a way as to detect an infinite loop. In the following example, simple logic has been built into a while function to detect an infinite loop error: <?php     $i = 5;     while ($i < 5) {         echo '$i <br>';         $i = $i++;         if ($i > 20000) { // Break when > 20,000 repititions             break;         }     } ?> Do ... While      The do ... while function is based on the while iterative control statem

PHP Data Types

 A data type identifies the characteristics that the assigned data will be interpreted by when interacting with the PHP programming language.  When a value is assigned a specific data type,  the PHP interpreter will work with the data based on the expected type of data it is.  For example, 27654 could be considered a numeric data type or it could be considered a string data type, such as in the zip code for a customer.  Even though you could perform mathematical equations on the zip code, it would make no sense to do so.  Thus,zip codes, even though they look like they would be numeric numbers should be identified as a string data type to eliminate problems such as zip codes that start with zero (ex. 08102).  Assigning the correct data type to the expected value is an important part of working with PHP.  There are three categories of data types in PHP: Scalar, Compound and Special.  PHP supports eight primitive types. Four scalar types: boolean integer float  (floati

Different Types of Output Methods in PHP

The strength of the web has been the ability for practically anyone to provide information (accurate or not) to anyone who is interested in viewing the web pages that contain that information.  With HTML, the static nature of the information being displayed limits the amount of true customization that can be provided; however, PHP provides truly dynamic content by creating custom content each time the web page is displayed.  To accomplish this display of information, PHP has four functions that make this happen:     •  print() -                     This statement is designed to provide user feedback; in other words, this function "prints" content to the HTML code which in turn will be displayed on the web page when called.  The print() function can display both raw strings and variables.  <?php      print('<p>This is the standard use of the print function.</p>');      print '<p>However, the parentheses are not necessary.</p&