Difference between revisions of "Learning PHP (1)"

From Wiki @ Karl Jones dot com
Jump to: navigation, search
(PHP files)
(See also)
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This article introduces [[PHP]] for the complete beginner.
+
This article introduces [[PHP]] for the beginner.
  
 
== Requirements ==
 
== Requirements ==
  
Getting started with [[PHP]] requires:
+
Learning and using [[PHP]] requires:
  
 
* A [[web server]] (or other [[computer]]) running the [[PHP interpreter]].
 
* A [[web server]] (or other [[computer]]) running the [[PHP interpreter]].
Line 25: Line 25:
 
== PHP and HTML ==
 
== PHP and HTML ==
  
...
+
PHP pages can contain HTML (and CSS, and JavaScript), which behaves the same as it does in [[Static web page|static web pages]].
 +
 
 +
== PHP code ==
 +
 
 +
'''PHP code''' is [[text]] consisting of one or more [[Statement (computer science)|statements]] in the [[PHP]] [[programming language]].
 +
 
 +
The examples below will introduce simple PHP statements.
 +
 
 +
== PHP page without PHP? ==
 +
 
 +
In principle, a PHP page could be all HTML, with no [[PHP code]].
 +
 
 +
This is not typically done, as it would defeat the purpose of PHP.
 +
 
 +
== PHP code islands ==
 +
 
 +
PHP code islands (or code blocks) contain PHP code.
 +
 
 +
PHP code islands must not include HTML, CSS or JavaScript;  using these languages inside a code island will trigger a PHP error.
 +
 
 +
You can use PHP code to generate text, including HTML, CSS, etc. See [[Generating text (PHP)]].
 +
 
 +
== Mixing code islands and HTML ==
 +
 
 +
You can freely mix HTML and code islands in a PHP page.
 +
 
 +
Code islands look like this:
 +
 
 +
<pre>
 +
<?php
 +
 
 +
?>
 +
</pre>
 +
 
 +
== Where does the PHP code go? ==
 +
 
 +
Beginners commonly ask:  ''Where does the PHP code island go in the page?''
 +
 
 +
Answer:
 +
 
 +
* If the code island contains PHP code which generates text, put the code island where you want the text to appear in the page.
 +
* If the code island does not generate content, the code island typically goes at the top of the page, although this is not a requirement.
 +
 
 +
== All PHP ==
 +
 
 +
A PHP page can be all PHP: one code island, no static HTML.
 +
 
 +
This is common with [[Web application|web applications]], such as [[WordPress]].
 +
 
 +
== Web template systems ==
 +
 
 +
[[Web application|Web applications]] commonly have a mix of:
 +
 
 +
* All-PHP pages
 +
* Pages which mix HTML with code islands according to a [[web template system]].
  
 
== See also ==
 
== See also ==
Line 33: Line 87:
 
* [[WordPress]]
 
* [[WordPress]]
  
== External links ==
+
[[Category:Learning]]
 +
[[Category:PHP]]

Latest revision as of 06:49, 23 April 2016

This article introduces PHP for the beginner.

Requirements

Learning and using PHP requires:

Web server with PHP interpreter

Most web servers, including the widely-used Apache HTTP Server, have the PHP interpreter pre-installed.

Text editor

You can use any text editor to edit PHP files.

PHP files

PHP files have the .php extension.

PHP and HTML

PHP pages can contain HTML (and CSS, and JavaScript), which behaves the same as it does in static web pages.

PHP code

PHP code is text consisting of one or more statements in the PHP programming language.

The examples below will introduce simple PHP statements.

PHP page without PHP?

In principle, a PHP page could be all HTML, with no PHP code.

This is not typically done, as it would defeat the purpose of PHP.

PHP code islands

PHP code islands (or code blocks) contain PHP code.

PHP code islands must not include HTML, CSS or JavaScript; using these languages inside a code island will trigger a PHP error.

You can use PHP code to generate text, including HTML, CSS, etc. See Generating text (PHP).

Mixing code islands and HTML

You can freely mix HTML and code islands in a PHP page.

Code islands look like this:

<?php

?>

Where does the PHP code go?

Beginners commonly ask: Where does the PHP code island go in the page?

Answer:

  • If the code island contains PHP code which generates text, put the code island where you want the text to appear in the page.
  • If the code island does not generate content, the code island typically goes at the top of the page, although this is not a requirement.

All PHP

A PHP page can be all PHP: one code island, no static HTML.

This is common with web applications, such as WordPress.

Web template systems

Web applications commonly have a mix of:

See also