Understanding PHP basics: Difference between revisions

From Leechfinger
Jump to navigationJump to search
Qais (talk | contribs)
Qais (talk | contribs)
Line 51: Line 51:
== Template ==
== Template ==
Templates are library files that is reusable code. You can include templates in your code via 4 methods: include, include_once, require, and require_once.
Templates are library files that is reusable code. You can include templates in your code via 4 methods: include, include_once, require, and require_once.
In a template file you will have your HTML code two variable: $title, $content. The title variable will be in the title section and content variable will be in the body section of the HTML. Save this file in templates/page.php. You will call this file from your index page and also put content in the two variables.
template/page.php
<syntaxhighlight lang="php" line>
<?php
$page=
"<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
$content
</body>
</html>";
?>
</syntaxhighlight>
Here is the index file that calls the template/page.
<syntaxhighlight lang="php" line>
<?php
//complete code for index.php
$title = "Test title";
$content = "<h1>Hello World</h1>
<br>
<h3>Good year tires</h3>";
//indicate the relative path to the file to include
require "templates/page.php";
echo $page;
?>
</syntaxhighlight>
[[Category:PHP]]
[[Category:PHP]]

Revision as of 17:38, 25 May 2024

Get PHP info

<?php
 phpinfo();
?>

Let's write our first PHP code.

<?php
 echo "Hello from PHP";
?>

Storing values in a variable

<?php
$myName = "Kooka";
$friendsName = "Loopa";
echo "<p>I am $myName and I have a friend called $friendsName.</p>";
?>

Display errors

Insert the following two lines at the top of your scripts to display errors.

error_reporting( E_ALL );
ini_set( "display_errors", 1);

HTML5 page with php

<?php 

$title = "Test title";
$content = "<h1>Hello World</h1>";

$page = "
<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
$content
</body>
</html>";

echo $page;
?>

If you view source on the above code you will only see the HTML code.

Template

Templates are library files that is reusable code. You can include templates in your code via 4 methods: include, include_once, require, and require_once. In a template file you will have your HTML code two variable: $title, $content. The title variable will be in the title section and content variable will be in the body section of the HTML. Save this file in templates/page.php. You will call this file from your index page and also put content in the two variables. template/page.php

<?php
$page= 
"<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
$content
</body>
</html>";
?>

Here is the index file that calls the template/page.

<?php
//complete code for index.php
$title = "Test title";
$content = "<h1>Hello World</h1>
<br>
<h3>Good year tires</h3>";
//indicate the relative path to the file to include
require "templates/page.php";
echo $page;
?>