What is PHP?
PHP is a popular scripting language used in web development. PHP is an acronym which stands for hypertext preprocessor. A PHP file often contains HTML, CSS, JavaScript, and PHP code so an understanding of HTML, CSS, and JavaScript is helpful for learning PHP. PHP is used to create dynamic web pages and it can collect data from forms and add, alter, or delete data in a database. A PHP script is executed in the server and an HTML response is sent to the browser.
What PHP can do?
There is so much we can do with PHP. Take a look at Facebook or WordPress.org.
Things PHP Can Do:
- PHP can generate dynamic page content.
- PHP can add, delete and modify data in your database.
- PHP can be used to control user-access (logins).
- PHP can collect form data.
- PHP works completely behind the scenes.
In WordPress:
PHP is usually used for getting and putting content to / from the WordPress database.
The data saved in the database includes:
- posts
- pages
- comments
- users.
WordPress theme files
contain a combination of PHP and HTML.
WordPress plugin files
contain primarily PHP code. They may have some HTML, CSS and JavaScript too.
PHP code
A PHP script is executed on the server, and the plain HTML result is sent back to the browser. And it is called PHP SYNTAX
Basic PHP Syntax
A PHP script can be placed anywhere in the document.
- PHP script starts with
<?php
and ends with?>
- PHP code goes inside PHP tags
<?php
// PHP code is placed between the tags.?>
- PHP files default file extension is “
.php
“. - PHP file normally contains HTML tags, and some PHP scripting code.
- PHP variable starts with the $ sign followed by the name of the variable (variable names are case-sensitive).
<?php
// PHP code goes here
?>
PHP Case Sensitivity
In PHP, keywords (e.g. if
, else
, while
, echo
, etc.), classes, functions, and user-defined functions are not case-sensitive.
PHP Comments
Single line comment. //Short comment
Multi-line comments. /* multiple line comment */
# This is a comment.
// and this too.
PHP Scripts
Copyright <?php echo date(‘Y’); ?></strong
> Osama.fr.
That code shows the dynamic copyright date in my website. Put this code in a PHP file to show the current year.
PHP code can be placed directly into the child theme’s template files.
Variables
A variable is a piece of information that has a name and a value. The name never changes and is used to identify the piece of information. The value of a variable can change.
The format for declaring a variable is as follows.
$variableName = variableValue
To use a variable, type $ followed by the variable name. Variables are not constant and can be rewritten using the same format as written above.
Data Types
- Strings: which is plain text.
- Integers: whole numbers. Integers can be positive, negative, or zero.
- Float: which are numbers with decimals.
- Boolean: true or false.
- null: used when something does not have a value.
Code Snippets
A programming term for a small region of re-usable source code, machine code, or text.
A small chunk of PHP code that you can use to extend the functionality of a website.
PHP Files
Although similar to HTML files, PHP files can include both PHP and HTML files. Also, HTML cannot perform computations like 1 + 1 = 2
PHP Include Files
PHP lets you include the contents of one file into another. Which is wonderful news for web designers, as it makes designing web pages a lot simpler and extra efficient.
PHP functions such as include()
or the WordPress feature get_template_part()
make it feasible to include web page templates in a web page.
In WordPress:
Any web page on a WordPress site, contains code for Template Files:
the header, the footer, the sidebar etc.. These files will be included in the page.
Header file called header.php
Sidebar(s) file called sidebar.php
Footer file called footer.php
PHP Functions
A set of code statements, that when it is called (i.e. executed), fires a specific task.
Re-usable functions can be defined in the Template files.
In WordPress
A theme’s functions.php file is located inside the folder called “themes.” in the /wp-content/ folder.
Inside functions.php file
starts with: <?php
The closing PHP tag ?>
is optional at the end of the file
functions.php file is not the place to put custom CSS code. This file is for PHP code snippets and functions.
NOTES on editing PHP functions files
PHP is not as forgiving as HTML. The tiniest mistake totally break any site and turning it inaccessible even to admins.
Before inserting a code, should make sure the previous function is over. The closing curly brace } indicates the end of a function.
In WordPress
ALWAYS back up your WordPress site before making any changes to the code
Code Snippets plugin is a better way to add customized code to a WordPress site. It offers you a safer way to add Snippets to functions.php file. Your code snippets can all be managed from the Manage page of the plugin, activate or deactivate them.
0 Comments