Basic PHP.
Culled from: http://www.w3schools.com/php/php_intro.asp
Note: There is some discusion about the validity of w3schools code. Their own warrantee states "W3Schools may contain inaccuracies or errors. Refsnes Data makes no guarantees regarding the accuracy of the site or its contents." Use their code carefully and double check what they say with better sources.
What You Should Already Know
Before you continue, you should have a basic understanding of the following:
- HTML
- Some scripting knowledge
What is PHP?
- PHP stands for PHP: Hypertext Preprocessor
- PHP is a server-side scripting language, like ASP
- PHP scripts are executed on the server
- PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid,
PostgreSQL, Generic ODBC, etc.) - PHP is open source software
- PHP is free to download and use
What is a PHP File?
- PHP files can contain text, HTML tags and scripts
- PHP files are returned to the browser as plain HTML
- PHP files have a file extension of ".php", ".php3", or ".phtml"
What is MySQL?
- MySQL is a database server
- MySQL is ideal for both small and large applications
- MySQL supports standard SQL
- MySQL compiles on a number of platforms
- MySQL is free to download and use
PHP + MySQL
- PHP combined with MySQL are cross-platform (for example, you can develop in
Windows and serve on a Unix platform)
Why PHP?
- PHP runs on different platforms (Windows, Linux, Unix, etc.)
- PHP is compatible with almost all servers used today (Apache, IIS, etc.)
- PHP is FREE to download from the official PHP resource:
www.php.net - PHP is easy to learn and runs efficiently on the server side
Where to Start?
If you're made it to Drupal, you've already started. Here's some basics.
Variables
Variables are used for storing values
When a variable is set, it can be used over and over again in your script
All variable names in PHP start with a $ symbol.
The correct way of setting a variable in PHP:
$variable = value;
Strings
String variables are used for values that contains character strings.
$txt = 'Hello world!';
Operators
Operators are the pieces of code that tell PHP a piece of what its doing, such as adding, subtracting, checking if two values are equal, checking if they are not equal, etc.
The list of PHP operators can be found: http://www.w3schools.com/php/php_operators.asp
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions.
The basic conditions:
- if
- else
- elseif
Other conditions - these are used to create loops:
- while
- foreach
An extension of this type of conditional logic is the 'switch' statement.
Printing, Returning and Looking at Values
- return
- print_r() and dsm()
Arrays and Objects
Arrays and Objects are very different in how they operate and what they are used for. However, there's enough similarity to cover them together for an introduction to PHP.
Arrays contain values that are keyed, eg: array('month' => 'Feb', 'year' => 2009);
Objects and arrays are both used to store sets of data, usually in a single variable. In Drupal, there are several common objects that contain values and arrays, that you will see used everywhere. Let's look at the $user object.
Write some code with the user object.