PHP Programming/Code Snippets
Tools
General
Sister projects
In other projects
| PHP Programming Code Snippets | Coding Standards |
Code snippets are useful for any beginners to learn code from.
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 thevalue 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"); wouldnot 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 return12:22:01 10/08/2006 on 10th August 2006 at 12:22:01.unlink("filename"); - Function that deletes the file specified infilename.<?php$variable1='beginning';//This is a comment after a variable being definedif($variable1=='beginning'){//If is a test to see if a variable has certain//value and initiates the wanted sequences if trueecho'Hello World!';//The echo displays to the page}else{echo'some code';}?>
Include OOP based examples, made by experienced developer
Basics, working only on PHP 5.
file_put_contents("filename", "Text to save"); - Functions that saves the text specified inText to save to the file specified infilename. Will overwrite existing file contents unless another parameter FILE_APPEND is added.E.g.file_put_contents("filename", "Text to save"); will writeText to save tofilename, but will overwrite existing text whereasfile_put_contents("filename", "Text to save", FILE_APPEND); will writeText to save tofilename, but willnot overwrite existing text (instead it appends).
| PHP Programming Code Snippets | Coding Standards |