
The PHP Tutorial for Beginners in One Page article is for programmers who wish to learn the fundamentals of PHP programming.
PHP is a programming language that runs on the server. When we request a PHP page from our browser, the server computer executes it. It does not run on our computer. Finally, the result is generated in HTML format.
What is PHP? PHP is a server side scripting language. Multiple databases are supported by PHP (MySQL, Informix, Solid, Oracle, Sybase, Generic ODBC etc.).
PHP is a loosely typed programming language.
Table of Contents
What are the usage of PHP?
PHP may be used for a variety of purposes. We’ve listed a few of the most significant goals below.
- Using PHP, we can generate dynamic page content.
- PHP allows us to create, open, read, write, remove, and close files on the server.
- It can be used to collect data from HTML forms.
- PHP has the ability to insert, remove, and alter data in our database.
- Some pages on our website might be restricted by PHP.
Why use PHP?
- PHP may be used on a variety of systems (Windows, Linux, Unix etc.).
- PHP is a server-side scripting language that can run on practically any platform (Apache, IIS etc.).
- Powerful, robust, and scalable (When numerous users use the system at the same time, it takes the minimum amount of time possible).
- It’s free and open source.
What do we need to run PHP?
- The most important thing is that we require a web server.
- The PHP engine (Interpreter).
- A database, preferably MySql, is required.
PHP Syntax
A PHP programming block is always preceded by the characters <?php and followed by the characters ?>. In the document, a PHP programming block can be put anywhere.
<?php
// Code goes here…
?>
How to write comments in PHP?
PHP accepts comments in the ‘C’, ‘C++’, and Unix shell formats.
1) Single Line comment in PHP
//.........................
#..........................
2) Multiline comment in PHP
/*.........................
..........................*/
How do I write first PHP program?
<html>
<body>
<?php
echo "Hello world";
?>
</body>
</html>
Things to Remember for PHP
- Every PHP statement must be enclosed by the tags <?php and?>.
- Each command must be followed by a semicolon (;).
- Variables and constants in PHP are case sensitive, while keywords like echo, if-else, and for are not.
Variable in PHP?
Text, strings, integers, and arrays are all stored in variables. When you declare a variable, you may use it again and again in your script. In PHP, all variables begin with the $ symbol.
Rules for declaring variables in PHP?
PHP have a rule to declare variables which you have to follow. This is very basic of php programming.
- The $ symbol precedes the variable name, which is then followed by the name of the variable.
- There is no command in PHP for declaring variables. When we initially assign a value to a variable, it is called a variable.
- A variable’s name must start with a letter or an underscore.
- Only alphanumeric letters and underscores (A-Z, a-z, 0-9, and _) are allowed in variable names.
- There should be no spaces in the name of a variable.
- Case matters when naming variables ($y and $Y are two separate variables).
How variables are declared in PHP give example?
$x=5;
$y=6;
$z=$x + $y;
Scope of Variables in PHP
The term scope refers to a variable’s accessibility, or its ability to be accessible from anywhere in our script. There are three scopes in PHP.
- Local Scope
- Global Scope
- Static Scope
Local Scope of variables in PHP
A variable declared within a PHP function is local, meaning it can only be accessed by the function itself.
<?php
function show() // Function definition
{
$x = 5;
echo $x;
}
show(); // Function calling
?>
Global Scope of variables in PHP
Global variables are those defined outside of the block. They may be accessible from anywhere in our script besides the function’s body.
For example, x has been defined as global in the code below, but it can only be accessible from the global area, not from the body of the function display.
<?php
$x = 5;
function show()
{
echo $x; // This line will given error;
}
show();
echo $x; // Correct
?>
Global Scope of variables in PHP
Variables defined with the static keyword are kept by PHP even after the function execution is over, and their prior values can be restored.
<?php
function show()
{
static $x = 5;
echo $x;
$x++;
}
show();
show();
show();
?>
Click here to learn Java Tutorial.
In conclusion, The article “PHP Tutorial for Beginners on One Page” is designed for the developers who want to learn PHP basic and advance. This page will be updated on a regular basis.
I hope you like the PHP Tutorial. Please email us at geekcer.code@gmail.com if you have any questions or concerns.