Jump to content

PHP Programming/Code Snippets

From Wikibooks, open books for an open world

Code snippets are useful for any beginners to learn code from.

PHP 4 & 5

[edit | edit source]

Basic Level

[edit | edit source]
  • echo "the text to print"; - This language construct will echo the text between the quotes. This is not a function but a language construct.
  • echo "$var"; - Notice the double quotation marks. Because double quotation marks are used, this will print the value of the variable. If $var="Bobby", this will output:

Bobby

  • echo '$var'; - Notice that the quotation marks are now single. This will output the literal keystrokes inside the quotes. The example will output:

$var

  • $var="Jericho";echo "Joshua fit the battle of $var."; - Other than substituting the value of a variable for the variable name (and one or two other minor items), double quotes will quote literal keystrokes. So this will output:

Joshua fit the battle of Jericho.

Again, if single quotes were used — 'Joshua fit the battle of $var'; — this would output:

Joshua fit the battle of $var.

  • echo $var; - If you only want to print the value of a variable, you don't need quotes at all. If the value of $var is "1214", the code will output:

1214

  • require "url"; - This language construct will include the page between the quotes. Can NOT be used with dynamic pages, e.g. require("main.php?username=SomeUser"); would not work. This is not a function but a language construct.
  • date("Date/time format"); - Function that returns a date from a Unix Timestamp - where H is the hour, i is the minutes, s is the seconds, d is the day, m is the month and Y is the year in four digits - e.g. date("H:i:s d/m/Y"); would return 12:22:01 10/08/2006 on 10th August 2006 at 12:22:01.
  • unlink("filename"); - Function that deletes the file specified in filename.

PHP 4

[edit | edit source]

Basic Level

[edit | edit source]
<?php
  $variable1 = 'beginning';
  //This is a comment after a variable being defined
  if ($variable1 == 'beginning') {
    //If is a test to see if a variable has certain
    //value and initiates the wanted sequences if true
    echo 'Hello World!';
    //The echo displays to the page
  }
else
{
echo 'some code';
}

?>

Include OOP based examples, made by experienced developer

PHP 5 Only

[edit | edit source]

Basic Level

[edit | edit source]

Basics, working only on PHP 5.

  • file_put_contents("filename", "Text to save"); - Functions that saves the text specified in Text to save to the file specified in filename. Will overwrite existing file contents unless another parameter FILE_APPEND is added.

E.g. file_put_contents("filename", "Text to save"); will write Text to save to filename, but will overwrite existing text whereas file_put_contents("filename", "Text to save", FILE_APPEND); will write Text to save to filename, but will not overwrite existing text (instead it appends).

  1. Input validation by Kgrsajid.
  2. Advanced Input validation by nemesiskoen.