Understanding PHP basics: Difference between revisions

From Leechfinger
Jump to navigationJump to search
Qais (talk | contribs)
No edit summary
Qais (talk | contribs)
 
(46 intermediate revisions by the same user not shown)
Line 26: Line 26:
ini_set( "display_errors", 1);
ini_set( "display_errors", 1);
</syntaxhighlight>
</syntaxhighlight>
== HTML5 page with php ==
<syntaxhighlight lang="php" line>
<?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;
?>
</syntaxhighlight>
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.<br>
;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>
== 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.<br>
In the following example $pageData and $jobData are objects, StdClass() is PHP's native class and title is the property.
<syntaxhighlight lang="php" line>
<?
$pageData = new StdClass();
$pageData->title = "Welcome to my kitchen";
/*
Many lines later
*/
$jobData = new StdClass();
$jobData->title = "Construction worker";
?>
</syntaxhighlight>
To get values from an object property, we must specify two things: which object and which properties to get. The syntax is as follows:<br> $objectName->propertyName;
== Our first dynamic website ==
We will need some directories for our files. /classes /css /templates /views.
We will first declare our class.
;classes/Page_Data.class.php
<syntaxhighlight lang="php" line>
<?php
class Page_Data {
    public string $title = "";
    public string $content = "";
    public string $css = "";
    public string $embeddedStyle = "";
}
?>
</syntaxhighlight>
The Page_Data class enables us to keep a placeholder for embedded styles in the page template and only assign an actual value to that property whenever we need a page with an embedded <style> element.<br>
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.<br>
;views/skills.php
<syntaxhighlight lang="php" line>
<?php
$info= "<h1>Skills and educational background</h1>
<p>Read all about my skills and my formal training</p>
";
?>
</syntaxhighlight>
;views/projects.php
<syntaxhighlight lang="php" line>
<?php
$info= "<h1>Projects I have worked on</h1>
<ul>
<li>Ahem, this will soon be updated</li>
</ul>";
?>
</syntaxhighlight>
Now we need to create a navigation for the two above views.
;views/navigation.php
<syntaxhighlight lang="php" line>
<?php
$nav= "
<nav>
    <a href='index.php?page=skills'>My skills and background</a>
    <a href='index.php?page=projects'>Some projects</a>
</nav>
";
?>
</syntaxhighlight>
Let's create our template page using objects.
;templates/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' />
$pageData->css
$pageData->embeddedStyle
</head>
<body>
$pageData->content
</body>
</html>";
?>
</syntaxhighlight>
We add our css for the site.
;css/layout.css
<syntaxhighlight lang="css" line>
nav {
    background-color: #CCCCDE;
    padding-top: 10px;   
}
nav a{
    display:inline-block;
    text-decoration:none;
    color: #000;
    margin-left: 10px;
}
nav a:hover{text-decoration: underline;}
</syntaxhighlight>
Finally, let's create our index.php file using objects.
;index.php
<syntaxhighlight lang="php" line>
<?php
//complete code for index.php
$nav = "";
$info = "";
include_once "views/navigation.php";
include_once "classes/Page_Data.class.php";
$pageData = new Page_Data();
$pageData->title = "Object oriented site";
$pageData->css = "<link href='css/layout.css' rel='stylesheet' />";
$pageData->content = $nav;
//changes begin here
$navigationIsClicked = isset($_GET['page']);
if ($navigationIsClicked ) {
    $fileToLoad = $_GET['page'];
} else {
    $fileToLoad = "skills";
}
include_once "views/$fileToLoad.php";
$pageData->content .= $info;
//end of changes
require "templates/page.php";
echo $page;
?>
</syntaxhighlight>
In our index.php page we used $_GET ''superglobal array'' to access the URL variable named page. We used ''isset()'' to make sure the variable exists, otherwise we will trigger a PHP error. This variable, page, does not have a dollar sign since it is not a PHP variable. It is a URL variable, which does not require a dollar sign.<br>
A ''conditional statement'' (if statement) will determine if the information contained in the parentheses is TRUE or FALSE. If $navigationIsClicked is TRUE, the if statement will execute any code contained within the curly brackets ({}).<br>
Our first dynamic site is working with ''strict naming convention''. The PHP files in our views directory have to be named skills and projects.<br>
Line 11 and 12 in the index.php indicate a default page to display when a user accesses the page.
[[Category:PHP]]
[[Category:PHP]]

Latest revision as of 21:32, 5 June 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 first dynamic website

We will need some directories for our files. /classes /css /templates /views.

We will first declare our class.

classes/Page_Data.class.php
<?php
class Page_Data {
    public string $title = "";
    public string $content = "";
    public string $css = "";
    public string $embeddedStyle = "";
}
 ?>

The Page_Data class enables us to keep a placeholder for embedded styles in the page template and only assign an actual value to that property whenever we need a page with an embedded <style> element.
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.

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

We add our css for the site.

css/layout.css
nav {
    background-color: #CCCCDE;
    padding-top: 10px;    
}
nav a{
    display:inline-block;
    text-decoration:none;
    color: #000;
    margin-left: 10px;
}
nav a:hover{text-decoration: underline;}

Finally, let's create our index.php file using objects.

index.php
<?php
//complete code for index.php
$nav = "";
$info = "";
include_once "views/navigation.php";
include_once "classes/Page_Data.class.php";
$pageData = new Page_Data();
$pageData->title = "Object oriented site";
$pageData->css = "<link href='css/layout.css' rel='stylesheet' />";
$pageData->content = $nav;
//changes begin here
$navigationIsClicked = isset($_GET['page']);
if ($navigationIsClicked ) {
    $fileToLoad = $_GET['page'];
} else {
    $fileToLoad = "skills";
}
include_once "views/$fileToLoad.php";
$pageData->content .= $info;

//end of changes
require "templates/page.php";
echo $page;
?>

In our index.php page we used $_GET superglobal array to access the URL variable named page. We used isset() to make sure the variable exists, otherwise we will trigger a PHP error. This variable, page, does not have a dollar sign since it is not a PHP variable. It is a URL variable, which does not require a dollar sign.
A conditional statement (if statement) will determine if the information contained in the parentheses is TRUE or FALSE. If $navigationIsClicked is TRUE, the if statement will execute any code contained within the curly brackets ({}).
Our first dynamic site is working with strict naming convention. The PHP files in our views directory have to be named skills and projects.
Line 11 and 12 in the index.php indicate a default page to display when a user accesses the page.