what do means by php?
PHP is an open-source, interpreted, and object-oriented scripting language that can be executed at the server-side. PHP is well suited for web development. Therefore, it is used to develop web applications (an application that executes on the server and generates the dynamic page.
- PHP stands for Hypertext Preprocessor.
- PHP is an interpreted language, i.e., there is no need for compilation.
- PHP is faster than other scripting languages, for example, ASP and JSP.
- PHP is a server-side scripting language, which is used to manage the dynamic content of the website.
- PHP can be embedded into HTML.
- PHP is an object-oriented language.
- PHP is an open-source scripting language.
- PHP is simple and easy to learn language.
Basic PHP Syntax:
A PHP script starts with <?php
and ends with ?>
:
<?php
// PHP code goes here
?>
example with programing:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP Features:
PHP is very popular language because of its simplicity and open source. There are some important features of PHP given below:
Install PHP:
To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software stack. It is available for all operating systems. There are many AMP options available in the market that are given below:
- WAMP for Windows
- LAMP for Linux
- MAMP for Mac
- SAMP for Solaris
- FAMP for FreeBSD
- XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.
PHP echo and print Statements
echo and print are language constructs, and they never behave like a function. Therefore, there is no requirement for parentheses. However, both the statements can be used with or without parentheses. We can use these statements to output variables or strings.
Difference between echo and print:
echo:
- echo is a statement, which is used to display the output.
- echo can be used with or without parentheses.
- echo does not return any value.
- We can pass multiple strings separated by comma (,) in echo.
- echo is faster than print statement.
print:
- print is also a statement, used as an alternative to echo at many times to display the output.
- print can be used with or without parentheses.
- print always returns an integer value, which is 1.
- Using print, we cannot pass multiple arguments.
- print is slower than echo statement.
PHP Variables:
In PHP, a variable is declared using a $ sign followed by the variable name. Here, some important points to know about variables:
- As PHP is a loosely typed language, so we do not need to declare the data types of the variables. It automatically analyzes the values and makes conversions to its correct datatype.
- After declaring a variable, it can be reused throughout the code.
- Assignment Operator (=) is used to assign the value to a variable.
Syntax of declaring a variable in PHP is given below:
$variablename=value;
PHP Variable: Declaring string, integer, and float:
<?php
$str="hello string";
$x=200;
$y=44.6;
echo "string is: $str <br/>";
echo "integer is: $x <br/>";
echo "float is: $y <br/>";
?>
output:string is: hello string
integer is: 200
float is: 44.6
PHP Variable Scope:
The scope of a variable is defined as its range in the program under which it can be accessed. In other words, “The scope of a variable is the portion of the program within which it is defined and can be accessed.”
PHP has three types of variable scopes:
- Local variable
- Global variable
- Static variable
Local variable:
The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. This means that these variables cannot be accessed outside the function, as they have local scope.
A variable declaration outside the function with the same name is completely different from the variable declared inside the function. Let’s understand the local variables with the help of an example:
<?php
function local_var()
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $num;
}
local_var();
?>
output:Local variable declared inside the function is: 45
Global variable:
The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.
Let’s understand the global variables with the help of an example:
<?php
$name = "anup RAJAK"; //Global Variable
function global_var()
{
global $name;
echo "Variable inside the function: ". $name;
echo "</br>";
}
global_var();
echo "Variable outside the function: ". $name;
?>
Output:
Variable inside the function: Anup Rajak
Variable outside the function: Anup Rajak
foreach loop for numeric value:
foreach loop for numeric vaule mainly using in key pair vaule that’s called is foreach loop…
Syntax
foreach ($array as$value) {
code to be executed;
}
PHP Functions:
PHP function is a piece of code that can be reused many times. It can take input as argument list and return value. There are thousands of built-in functions in PHP.
Advantage of PHP Functions:
Code Reusability: PHP functions are defined only once and can be invoked many times, like in other programming languages.
Less Code: It saves a lot of code because you don’t need to write the logic many times. By the use of function, you can write the logic only once and reuse it.PlayNextUnmute
PHP User-defined Functions:
We can declare and call user-defined functions easily. Let’s see the syntax to declare user-defined functions.
Syntax
- function functionname(){
- //code to be executed
- }
PHP Functions Example:
?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>
output:-hello php function
PHP Function Arguments:
We can pass the information in PHP function through arguments which is separated by comma.
PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-length argument list.
Let’s see the example to pass single argument in PHP function.
<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>
output:-Hello Sonoo
Hello Vimal
Hello John