PHP Syntax: A Complete Guide For Beginners -w9school
PHP is a server-side scripting language. Enclosed in <?php ?> tags, it handles variables, loops, conditionals, functions, arrays, objects, and integrates with HTML for dynamic web content.
PHP Syntax |
PHP (Hypertext Preprocessor) is a widely used server-side scripting language for web development. It's embedded within HTML code and executed on the server, generating dynamic web content. Here's an overview of PHP syntax: Tags and Output:
Example
Variables:
Example
Data Types:
Strings:
Example
Operators:
Example
Conditional Statements:
Example
Loops: PHP provides various loop types: Example
Functions:
Example
Arrays:
Example
Superglobals:
Include and Require:
Example
Classes and Objects:
Example
Exception Handling:
Example
Comments:
Example
Remember that this is just a brief overview of PHP syntax. PHP has a vast array of functions, libraries, and advanced features for various web development tasks. It's recommended to refer to official PHP documentation or tutorials for more in-depth understanding and examples. |
Basic PHP Syntax |
On the server, a PHP script is run, and the outcome is transmitted back to the browser as plain HTML. Anywhere in the document is a good spot to insert a PHP script. PHP scripts begin with the syntax : Example
".php" is the standard PHP file extension. HTML tags and a little amount of PHP scripting code are commonly seen in PHP files. Here's an example of a basic PHP file with a PHP script that uses the built-in PHP function "echo" to produce the word "Hello World!" on a web page: Example
|
PHP Case Sensitivity |
In PHP, case is not taken into account when using keywords, classes, functions, or user-defined functions (such as 'if', 'else', 'while', 'echo', etc.). All three of the echo statements in the following example are equal and legitimate: Example
The value of the '$color' variable will only be displayed in the first sentence of the example below! This is because to the fact that '$color', '$COLOR', and '$coLOR' are each regarded as a separate variable: Example
|
Comments in PHP |
A line of PHP code that is not run as part of the program is referred to as a comment. It exists just to be read by a person who is studying the code.One can utilize comments for:
PHP offers a number of commenting options: Example Syntax for single-line comments:
Example Syntax for multiple-line comments:
Example Using comments to omit code from the file:
|
PHP Variables |
PHP variables are used to store and manipulate data. They are preceded by a dollar sign ( Here are examples of variable declarations and usage: Example
Variables can store various data types, and their values can be manipulated, displayed, and used in calculations within PHP code.The "containers" for storing information are variables. |
Creating (Declaring) PHP Variables |
A variable in PHP begins with the '$' sign, followed by the variable name: Example
Following the execution of the aforementioned statements, the variables '$tx't, '$x', and '$y' will each contain the values Hello world!, 5, and 10.5 respectively. Reminder: Always enclose text values in quotes when assigning them to variables. Notably, PHP lacks a command for declaring variables in contrast to other programming languages. As soon as you first give it a value, it is created.
PHP VariablesA variable's name might be short (like x and y) or longer (like age, carname, or total_volume).PHP variable rules:
Output VariablesWhen displaying data on the screen, the PHP 'echo' command is frequently utilized. The example that follows will demonstrate how to print text and a variable: Example
The output from the following example will be the same as that from the previous example:
The total of two variables will be displayed in the example below:
A loosely typed language is PHP. Note that we did not need to inform PHP of the variable's data type in the example above. Depending on the value of the variable, PHP automatically assigns a data type to it. Due to the fact that the data types are not tightly defined, it is feasible to add a string to an integer without encountering any difficulties. Type declarations were added in PHP 7. By selecting the stringent requirement, it will throw a "Fatal Error" on a type mismatch and provides the opportunity to define the data type anticipated when declaring a function. |
PHP Variables Scope |
Variable declarations in PHP are allowed at any point in the script.A variable's scope is the area of the script where it can be used or referenced. There are three distinct variable scopes in PHP:
Global and Local ScopeThe only place a variable declared outside of a function can be accessed is outside of that function and has a GLOBAL SCOPE: Example Variable with global scope:
A variable declared within a function has a LOCAL SCOPE and can be accessed exclusively from within that function: Example Variable with local scope:
PHP The global KeywordTo access a global variable from within a function, use the 'global' keyword. To accomplish this, place the global keyword (inside the function) before the variables: Example
Additionally, PHP keeps track of all global variables in the array '$GLOBALS[index]'. The variable name is stored in the index. This array can be used to directly update global variables and is also reachable from within methods. The above illustration can be rewritten as follows: Example
PHP The static KeywordNormally, all of a function's variables are removed when it has been finished or run. However, there are situations when we want a local variable to remain in place. It is required for a different task. Use the 'static' keyword when you first declare the variable to do this: Example
After that, each time the function is called, the data from the previous call will still be there in that variable. Note that the variable is still specific to the function. |
What's Your Reaction?