IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
PHP
Search PHP Group:

Advanced
-Collapse +Expand PHP Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBPHP Knowledge Base  Print This    Code Snippet DB All Groups  

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.

Contribute a Code Snippet
Expand All

19 PHP & Delphi for PHP Code Snippets

Group: PHP & Delphi for PHP


Topic: Beginners Corner

-Collapse +Expand 1. PHP Assignment (=)
 

PHP uses = for it's assignment operator.

$fullname = "Randy Spitz";
$age = 38;
Posted By Mike Prestwood, Post #101895, KB Topic: Beginners Corner
-Collapse +Expand 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>";
Posted By Mike Prestwood, Post #101660, KB Topic: Beginners Corner
-Collapse +Expand 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.
*/
Posted By Mike Prestwood, Post #101537, KB Topic: Beginners Corner
-Collapse +Expand 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";
}
Posted By Mike Prestwood, Post #101881, KB Topic: Beginners Corner
-Collapse +Expand 5. PHP Deployment Overview
 

With PHP, you simply copy your files to a web server that is capable of running PHP pages.

Posted By Mike Prestwood, Post #101951, KB Topic: Beginners Corner
-Collapse +Expand 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.

Posted By Mike Prestwood, Post #101511, KB Topic: Beginners Corner
-Collapse +Expand 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."; 
};
Posted By Mike Prestwood, Post #101880, KB Topic: Beginners Corner
-Collapse +Expand 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";
}
Posted By Mike Prestwood, Post #101524, KB Topic: Beginners Corner
-Collapse +Expand 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.
};
Posted By Mike Prestwood, Post #101888, KB Topic: Beginners Corner
-Collapse +Expand 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).

Posted By Mike Prestwood, Post #101724, KB Topic: Beginners Corner
-Collapse +Expand 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).

Posted By Mike Prestwood, Post #101657, KB Topic: Beginners Corner
-Collapse +Expand 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.
";
Posted By Mike Prestwood, Post #101616, KB Topic: Beginners Corner
-Collapse +Expand 13. PHP Unary Operators
 

A unary operator operates on only one value.

PHP Examples:

  • ! negation operator
  • ++ increment operator
  • -- decrement operator
Posted By Mike Prestwood, Post #101561, KB Topic: Beginners Corner
-Collapse +Expand 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.
";
Posted By Mike Prestwood, Post #101615, KB Topic: Beginners Corner



Topic: Language Reference

-Collapse +Expand 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;
}
Posted By Mike Prestwood, Post #101635, KB Topic: Language Reference
-Collapse +Expand 16. PHP Overloading
 

PHP

  • Operator - No.
  • Method -

 

Posted By Mike Prestwood, Post #101466, KB Topic: Language Reference



Topic: PHP

-Collapse +Expand 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!";
}
?>
Posted By Mike Prestwood, Post #101509, KB Topic: PHP
-Collapse +Expand 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 . ".";
Posted By Mike Prestwood, Post #102185, KB Topic: PHP
-Collapse +Expand 19. PHP Development Tools
 

Many developers just use a text editor. There are many PHP editors available including phpDesigner, and Delphi for PHP.

Posted By Mike Prestwood, Post #101634, KB Topic: PHP
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2023 Prestwood IT Solutions.   [Security & Privacy]