Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

PHP Programming/Code Snippets

From Wikibooks, open books for an open world
<PHP Programming
Alternative Hungarian NotationPHP Programming
Code Snippets
Coding Standards

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 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 4

[edit |edit source]

Basic Level

[edit |edit source]
<?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';}?>

OOP

[edit |edit source]

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 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).

OOP

[edit |edit source]
  1. Input validation byKgrsajid.
  2. Advanced Input validation by nemesiskoen.


Alternative Hungarian NotationPHP Programming
Code Snippets
Coding Standards
Retrieved from "https://en.wikibooks.org/w/index.php?title=PHP_Programming/Code_Snippets&oldid=3676173"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp