Understanding PHP basics: Difference between revisions

From Leechfinger
Jump to navigationJump to search
Qais (talk | contribs)
Qais (talk | contribs)
Line 131: Line 131:
?>
?>
</syntaxhighlight>
</syntaxhighlight>
Let's create our template page using objects.
;template/page.php
<syntaxhighlight lang="php" line>
<?php
$page = "<!DOCTYPE html>
<html>
<head>
<title>$pageData->title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8' />
</head>
<body>
$pageData->content
</body>
</html>";
?>
</syntaxhighlight>
Finally, lets create our index.php file.
Finally, lets create our index.php file.
<syntaxhighlight lang="php" line>
<syntaxhighlight lang="php" line>

Revision as of 00:21, 26 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;
?>

Commenting your code

For one line comments you can use two front slashes // and for a block of code you can /* to at the beginning and end it with */. ==

Object-oriented

An object is a container that consists of variables/properties and functions/methods. For example and object can contain a variable for address and a function that generates directions to drive to that location. A class, which declares the content of the object, is similar to a blueprint of a house. The blueprint provides many details of the house but it is not the actual house. For an object to exist, it must be declared using a class. All objects exist in memory and are released when the program ends.
In the following example $pageData and $jobData are objects, StdClass() is PHP's native class and title is the property.

<?
$pageData = new StdClass();
$pageData->title = "Welcome to my kitchen";
/*
Many lines later
*/
$jobData = new StdClass();
$jobData->title = "Construction worker";
?>

To get values from an object property, we must specify two things: which object and which properties to get. The syntax is as follows:
$objectName->propertyName;

Our dynamic website

Our dynamic website will have different pages with different views so we need to create a views folder and save all views files in it.

views/skills.php
<?php
$info= "<h1>Skills and educational background</h1>
<p>Read all about my skills and my formal training</p>
";
?>
views/projects.php
<?php
$info= "<h1>Projects I have worked on</h1>
<ul>
<li>Ahem, this will soon be updated</li>
</ul>";
?>

Now we need to create a navigation for the two above views.

views/navigation.php
<?php
$nav= "
<nav>
    <a href='index.php?page=skills'>My skills and background</a>
    <a href='index.php?page=projects'>Some projects</a>
</nav>
";
?>

Let's create our template page using objects.

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

Finally, lets create our index.php file.

<?php
//complete code for index.php
include_once "views/navigation.php";
$pageData = new stdClass();
$pageData->title = "Thomas Blom Hansen: Portfolio site";
$pageData->content = $nav;
require "templates/page.php";
echo $page;
?>