PHP Code Snippets Page
These Code Snippets are contributed by you (our online community members). They are organized by our knowledge base topics. Specifically, by the PHP sub-topics.
|
19 PHP & Delphi for PHP Code Snippets
Group: PHP & Delphi for PHP
Topic: Beginners Corner
|
1. PHP Assignment (=) |
|
PHP uses = for it's assignment operator.
$fullname = "Randy Spitz"; $age = 38;
|
|
2. PHP Associative Array |
|
Declare associative array with initial known values. You can also add to associative array. (You can just assign values without ever declaring it too!)
$prices = array( 'Tires'=>100, 'Spark Plugs'=>4 ); $prices['Oil'] = 10;
echo "Tires=" . $prices['Tires'] . "<br>"; echo "Oil=" . $prices['Oil'] . "<br>";
|
|
3. PHP Comments (# or // or /* ... */) |
|
Commenting Code Use the multi-line to comment out large blocks of code and to write multiple line comments.
#This is a comment in PHP.
//This is too!
/* This is a multi-line comment. */
|
|
4. PHP Comparison Operators (==, != or <>) |
|
Common comparison operators:
== |
equal |
!= or <> |
not equal |
< |
less than |
> |
greater than |
<= |
less than or equal |
>= |
greater than or equal |
PHP 4 and above also offers === for indentical (equal plus same type) and !== for not identical (not equal or not same type).
//Does PHP evaluate the math correctly? No! if (.1 + .1 + .1 == .3) { echo "correct"; } else { echo "not correct"; }
|
|
5. PHP Deployment Overview |
|
With PHP, you simply copy your files to a web server that is capable of running PHP pages.
|
|
6. PHP File Extensions (.php) |
|
.php is the default extension for PHP although some developers will change the default extension in an effort to add an additional security level. If your code is tied to a particular version of PHP then some developers at the major PHP version number to the extension as in .php3, .php4, .php5, etc.
.phtml is also sometimes used especially for files that contain both HTML and Perl code.
|
|
7. PHP If Statement (if..elseif..else) |
|
The PHP if statement consists of using if, elseif, and else.
$x = 8; if ($x == 10) { echo "x is 10."; } elseif ($x < 10) { echo "x is less than 10."; } else { echo "x must be greater than 10."; };
|
|
8. PHP Literals (quote or apostrophe) |
|
In PHP you can use quotes, or apostraphies as in "Prestwood", and 'Prestwood'. Use a slash in front of a quote or apostrophe to embed same type as in \' and \".
echo "Mike's drums are over there.<br>"; echo 'Mike said, "hi!"<br>'; //Does PHP evaluate this simple //floating point math correctly? No! If ((.1 + .1 + .1) == .3) { Echo "Correct"; } Else { Echo "Not correct"; }
|
|
9. PHP Logical Operators (and, &&, or, ||, !, Xor) |
|
PHP logical operators:
and, && |
and, as in this and that |
or, || |
or, as in this or that |
! |
Not, as in Not This |
Xor |
either or, as in this or that but not both |
#Given expressions a, b, c, and d: if !((a && b) && (c || d)) { #Do something. };
|
|
10. PHP Overview and History |
|
PHP is a hybrid language with both traditional PHP and OOP features. PHP is widely-used general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP has been used to create some amazing web content, particularly outstanding message boards.
Target Platforms: PHP is most suitable for creating websites targeting any browser (any web server with PHP installed).
|
|
11. PHP Report Tools Overview |
|
Because website development targets a client browser (a document interfaced GUI), a common solution is to simply output an HTML formatted page with black text and a white background (not much control but it does work for some situations).
|
|
12. PHP String Concatenation (.) |
|
PHP uses a period (.) known as a dot to concatenate strings.
$fname = "Mike"; $lname = "Prestwood";
$fullname = $fname . $lname . " ";
echo "My name is " . "Mike. ";
|
|
13. PHP Unary Operators |
|
A unary operator operates on only one value.
PHP Examples:
- ! negation operator
- ++ increment operator
- -- decrement operator
|
|
14. PHP Variables ($x = 0;) |
|
PHP is a loosely typed language. No variable types in PHP. Declaring and using variables are a bit different than in other languages. In PHP, you identify and use a variable with a $ even within strings!
You assign by reference with & as in &$MyVar.
$fullname = 'Mike Prestwood'; $FullName = 'Wes Peterson'; //This is a different variable! $Age = 38; $Weight = 162.4;
echo "Your name is $fullname. "; echo "You are $Age and weigh $Weight. ";
|
Topic: Language Reference
|
15. PHP Custom Function (function) |
|
PHP uses functions and loosely typed parameters. Function definitions can come before or after their usage so my preference when mixing PHP in with a mostly HTML page is to put the functions after the </html> tag.
function sayHello($pName) { echo "Hello " . $pName . "<br>"; } function add($p1, $p2) { return $p1 + $p2; }
|
|
16. PHP Overloading |
|
|
Topic: PHP
|
17. PHP Code Blocks ({ }) |
|
In .PHPhtml pages, you embed PHP code between <?PHP and ?>.
For PHP, JavaScript, Java,and C++, I prefer to put the first { at the end of the first line of the code block as in the example above because I see morePHP codeformatted that way (and on PHP.Net).
PHP Alternative Syntax
Although I don't like to use it, PHP offers an alternative syntax for if, while, for, foreach, and switch. These code blocks are surrounded by statement ending keywords that all use End with camel caps such as endif, endwhile, endfor, endforeach,and endswitch.
<?PHP $x = "Yes"; //Simple if If ($x == "Yes") echo "hello world"; //If with a block of code.
If ($x == "Yes") { echo "Hello world"; echo "I am a PHP coder!"; } ?>
|
|
18. PHP Constants (define) |
|
In PHP, you declare constants using the define keyword: define("CONST_NAME", "Value");
Constants in PHP are case sensitive. A common standard in PHP is to use all-uppercase letters, with underscores to separate words within the name.
define('FULL_NAME', 'Mike Prestwood'); define("AGE", 25); echo "Your name is " . FULL_NAME . "."; echo "You are " . AGE . ".";
|
|
19. PHP Development Tools |
|
|
|
|