0%
Loading ...

PHP Basics

This category contains tutorials related to PHP Basics tutorials.

Function in PHP

In very simple language a function is a block of code that can be used to perform a specific task. In addition, we can use a function multiple times without writing it again and again. For example, you want to perform some mathematical task in your code several times. It means you have to write code several times to perform that mathematical task. But with the help of the function, you can write your mathematical task code only once and you can use it any number of times. Advantage: – Before digging deep into this topic. Here is a list of a few advantages of using functions in PHP. Code Reusability: We can call a function any number of times after defining it once. Less Code: When we use functions in code. It reduces the length of the overall code(program). Easy to Understand: Because we separate different programming logics into different functions so it becomes so easy to understand and maintain the code. Defining a Function in PHP: – Let’s check how to make and call a function in PHP Program. To make a function just write the function name with () and then write the body of the function. <?php // Defining function function hello(){ echo “Hello World<br>”; } // Calling function hello(); hello(); ?> Output: – Hello World Hello World After following the above Program example you would have a clear idea of how we can define and call a simple function in PHP. But do you know that we can also pass values to a function to perform some task? When we create(define) a function that can receive values this kind of function is known as Parameterised Function. Function with Parameter: Sometimes we need data to process in function. We can send data to a function while calling that function. Check the example below to understand it. <?php $name=”Owlbuddy”; // Defining function function hello($n){ echo “Hello “.$n.”<br>”; } // Calling function hello($name); hello($name); ?> Output: Hello Owlbuddy Hello Owlbuddy   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Function in PHP Read More »

Arrays in PHP

In simple words, an Array is a variable that makes us able to store more than one same kind of value using a single variable name. For example, you want to save the name of a hundred students in variables. It means you have to make 100 variables. But it makes your code so complicated and It is also time-wasting. Instead of spending so much time on making variables, you can store all 100 names of students in a single variable using an array. Here we will learn the basics of array-like how to define an Array and how to Initialize an Array in PHP etc. In an Array, values store using a single variable name. But we use the index number to add or fetch the value from Array. For example, you want to store the name of a hundred students in the student Array. Then you have to add values like the name of the first student on the 0 indexes of the student array, and the name of the next student on the 1st index like this. Defining and Initializing an Array: Defining an Array in PHP is so simple. The simplest way to define an array using the array() function. Check the example below to understand it clearly. <?php // Defining and Initializing an Array $students = array(“Ali”, “Sameer”, “Mohan”); ?> Fetching Values from Array: Fetching values from the array is so easy there is a print_r() function. By using this function you can fetch values and structure of the array. <?php // Defining and Initializing an Array $students = array(“Ali”, “Sameer”, “Mohan”); // Display the students array print_r($students); ?> In case you want to fetch values from the array and see the detailed structure of the array. There is another function var_dump() you can use this. Check the program below to check var_dump(); <?php // Defining and Initializing an Array $students = array(“Ali”, “Sameer”, “Mohan”); // Display the students array var_dump($students); ?> Types of Array in PHP: There is a total of three types of arrays in PHP. We will try to understand each type of array using Examples. These three types are as follows Indexed array Associative array Multidimensional array Indexed array: Indexed Arrays are simple arrays in which we save values on the index using the array() function as we saw in our previous examples. <?php // Defining and Initializing an Array $students = array(“Ali”, “Sameer”, “Mohan”); $marks[0]=80; $marks[1]=90; $marks[3]=85; print_r($students); print_r($marks); ?> Associative array: In an associative array, we save values in the key-value form. For example “Ali”=>80, “Sameer”=>90, “Mohan”=>85. Check this program <?php // Defining and Initializing an Array $stumarks = array(“Ali”=>80, “Sameer”=>90, “Mohan”=>85); $marks[“Ali”]=80; $marks[“Sameer”]=90; $marks[“Mohan”]=85; print_r($stumarks); print_r($marks); ?> Multidimensional array: Multidimensional Arrays are also known as an array of arrays. Mean instead of storing values on the index of an Array we save an array on the indexes of an array. Check the following example. <?php $studentInfo = array( array( “name” =–> “ali”, “marks” => 80, ), array( “name” => “Sameer”, “marks” => 90, ), array( “name” => “Mohan”, “marks” => 85, ) ); print_r($studentInfo); ?>   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Arrays in PHP Read More »

Operators in PHP

Operators are special symbols that tell to PHP processor to do some special operations. For example, there is a + operator which tells PHP Processor to add two numbers (4+5). There are various operators available in PHP and they are divided into some categories. List of Categories: Arithmetic Operators Comparison Operators Assignment Operators Inc/Dec Operators Logical Operators String Operators Array Operators Now we will learn about each category in detail. We will learn which operators are in each category and how they work. Arithmetic Operators: – There is a total of five operators in this category. Arithmetic operators are used to perform some basic mathematical operations. Operators in this category are +,-, /, *, %. Checklist of Arithmetic Operators. Operator Name Description Example + Addition Add Two Number 10+5 – Subtraction Subtract the second number from the first number 10-5 * Multiplication Multiply Two Number 10*5 / Division Division of Two Number 10/5 % Modulus Return remainder after Division 10%5 Comparison Operators: – Comparisons operators are used to compare two numbers. For example to check which number is greater than the given numbers (10>5). The result of comparison operators comes in Boolean form mean 10>5 result of this would be true. The list of operators in this category is as follows. Operator Name Description Example == Equal Return true if the first value would equal to second. 10==10 === Identical Return true if both operands would be of the same data type. 10===10 != Not equal Return true if both values are not the same. 10!=5 !== Not identical Return true if both values would not be of the same type. 10!===5 < Less than Return true if the first value will be less than second 10<20 > Greater than Return true if the first value will greater than second 10>5 <= Less than or equal to Return true if the first value will be less than or equal to second 10<=5 >= Greater than or equal to Return true if the first value will be greater than or equal to second 10>=5 Assignment Operators: – The most common Assignment operator is =. Assignment operators are used to assign value to variables. There is a total of six assignment operators check this list. Operator Name Description Example = Assign assign value to variables $num=10 += Add and assign Assign a value to the variable after adding a value on the right-hand side in the previous value of the variable. num+=10 ($num = $num+10) -= Subtract and assign Assign a value to the variable after subtracting the value on the right-hand side from the previous value of the variable. $num-=10 ($num=$num-10) *= Multiply and assign Assign a value to the variable after multiplying the value on the right-hand side with the previous value of the variable. $num*=10 ($num=$num*10) /= Divide and assign quotient Assign a value to the variable after dividing the previous value of the variable with the value on the right hand. $num/=10 ($num=$num/10) %= Divide and assign modulus Assign the remainder to the variable after dividing the previous value of the variable with the value on the right hand $num%=10 ($num=$num%10) Inc/Dec Operators: – Increment and Decrements are unary operators means it only works with a single operand. These operators are used to increase or decrease the value of a variable by one. Both Inc and Dec come in two types pre-increment and post-increment. Operator Name Description Example ++ Increment Increment the value of $a by one, then return $a Return $a, then increment the value of $a by one ++$a $a++ — Decrement   Decrement the value of $a by one, then return $a Return $a, then Decrement the value of $a by one –$a $a– Logical Operators: – Logical Operators are used to check logical relations between operands. These operators are used to perform bit-level operations. Operator Name Description Example and And Return TRUE if both $op1 and $op2 are true $op1 and $op2 Or Or Return TRUE if either $op1 or $op2 is true $op1 or $op2 xor Xor Return TRUE if either $op1 or $op2 is true but not both $op1 xor $op2 ! Not Return TRUE if $op1 is not true ! $op1 && And Return TRUE if either $op1 and $op2 are true $op1 && $op2 || Or Return TRUE if either $op1 or $op2 is true $op1 || $op2 String Operators: – String operators are used to perform some special operations with Strings. Operator Name Description Example . Concatenation Concatenate $str1 with $str2 $str1 . $str2 .= Concatenation and Assignment First will concatenate $str1 with $str2, then assign the final concatenated string to $str1, $str1 .= $str2 Array Operators: – Array operators are used to perform special operations with Arrays. Operator Name Description Example + Union Union of $num1 and $num2 $num1 + $num2 == Equality Return TRUE if $num1 and $num2 have same key/value pair $num1 == $num2 != Inequality Return TRUE if $num1 is not equal to $num2 $num1 != $num2 === Identity Return TRUE if $num1 and $num2 have the same key/value pair of the same type in the same order $num1 === $num2 !== Non-Identity Return TRUE if $num1 is not identical to $num2 $num1 !== $num2 <> Inequality Return TRUE if $num1 is not equal to $num2 $a <> $b Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Operators in PHP Read More »

Strings in PHP

The string is commonly described as a collection of characters. Mean a string can be a collection of numbers, alphabets, or special symbols for example “Hello World”. In PHP you can define a string in the program in two ways by using single quotes(‘ ‘) or by using double(” “) quotes. Here we learn about strings, basic functions in strings, escape-sequence, and the difference between single and double-quote strings. Difference between Single and Double Quote Strings: – We can write strings in PHP in both single and double quotes. But there is a certain difference between both. If you write a string in single quotes it will take string literally. Suppose you have a variable $num=10; and if you write something like this ‘Value of num=$num’. In this case, it will show the same. But if you will write this in double quotes the variable name will be changed with its value. To understand it more clearly follow the given example. <?php $num =10; echo ‘value of num = $num’; echo “value of num = $num”; ?> Escape-Sequence in strings: – These are some special escape sequences in PHP that we can use with strings. These escape sequences get replaced with special values. Check these escape sequences. \n is replaced with the newline character. \\ is replaced with a single backslash. (\) \r is replaced with the carriage-return character. \t is replaced with the tab. \” is replaced with a single double-quote (“). \$ is replaced with the dollar sign itself ($). Basic string Functions: – Here we will check some of the basic functions with would so helpful to you while working with strings. Here is like of some basic string functions in PHP. strlen(): function to check the total number of characters in a string. str_replace(): function to replace part or parts of a string with another string. str_word_count(): function to check the total number of words in a string. strrev(): function to reverse a string in PHP. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Strings in PHP Read More »

Data Types in PHP

PHP supports a total of eight data types available in PHP that are integer, float, string, boolean, array, object, resource, and NULL. These data types are used to define what kind of data a variable can store. Now we will check out every data type in detail with the help of examples. Integer: – In an integer data type variable, we can store integers mean decimal numbers, octal numbers, and hexadecimal numbers. These numbers may be signed or unsigned depending on requirements means as 10 or +10. <?php $num1 = 123; // integer number echo $num1; echo “<br–>”; $num2 = -123; // signed integer echo $num2; echo “<br>”; $num3 = 0x1A; // hexadecimal number echo $num3; echo “<br>”; $num4 = 0123; // octal number echo $num4; ?> Float: – Using this data type we can store decimal values like 2.5 or 3.2555 etc. <?php $num = 1.234; echo $num; echo “<br>”; ?> String: – In strings data type we can store a sequence of characters, special symbols, or numbers. To store any value in string type value must be enclosed in single or double quotes like ‘Owlbuddy’ or “Owlbuddy”. <?php $str1 = ‘Owlbuddy’; echo $str1; echo “<br>”; $str2 = “Owlbuddy”; echo $str2; echo “<br>”; ?> Boolean: – Boolean can hold only two values which are true or false. You can not store any other value in the boolean type. Boolean is so helpful while working with conditional statements. <?php $flag = true; echo $flag; ?> Array: – The array can be used to store the collection of similar kinds of data in a single variable. Suppose you want to store the age of 10 students you can easily do this using Array. <?php $colors = array(“Red”, “Green”, “Blue”); echo $color[0]; ?> Object: – The object allows us to store different kinds of data at once for example you created an object called Chair. The object Chair can be used to store data about a chair such as chair_height, chair_Price, and chair_color. Apart from storing data in an object you also mention relation functions in an object. <?php // Class definition class car{ // properties public $name = “BMW”; // methods function show_name(){ return $this—>$name; } } // Create object from class $car1 = new car; ?> Resource: – This type of variable is used to hold a reference to an external resource. <?php // Open a file for reading $path = fopen(“file.txt”, “r”); echo “<br>”; ?> Null: – This data type is used to represent any empty variable. <?php $var = NULL; echo $var; ?>   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Data Types in PHP Read More »

Constants in PHP

We already learned about variables. I assume you would have a clear idea about variables. As we know the value of a variable can be changed numerous times throughout the program. but on the other hand value of constants can not change. Constants are also used to store data like variables but once constants get initialized values can not be changed. With the help of constants, we have stored data that would not change in the whole program for example store data like DB_username, DB_password, or BASE_URL in constants. Constants help us to make sure that our data would not change throughout the whole program even if not by mistake by the programmer. If you define a constant in a program the only way to change the value is to change the value where you define the constant for the very first time in the program. In PHP we can define constants using the define() function. This function accepts two parameters first one is the name of the constant and the second is the value of the constant. After defining the constant you can access it anywhere in the program by using its name. Please check the following example program to understand constants more clearly. <?php //defining constant Here define(“language”, “PHP”); //accessing constant with name echo ‘I Love ‘ . language; ?>   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Constants in PHP Read More »

Variables in PHP

Variables are used to store data like strings or numbers for example min_age=18. The value of the variable can be changed numerous times throughout the code. It is so easy to define variables in PHP just write $sign and then a variable name like this $min_age=18. You do not even need to mention the data type of variable. Because PHP will automatically choose the correct data type of variable according to the value stored in a variable. PHP is a Loosely Typed language: – PHP is a programming language known for its loose typing. This means that in PHP, variables can easily switch between different data types without needing explicit type declarations. While this flexibility can lead to some unexpected issues, it also allows developers to work quickly and creatively, making PHP a popular choice for web development, whether you’re building an e-commerce website or a content management system. Declaring a Variable: – Declaring a variable means announcing its existence and specifying its data type. When you declare a variable, you are telling the computer that you intend to use a specific name for a storage location in memory. Initializing a Variable: – Initializing a variable means giving it an initial value at the time of declaration or later in your code. It’s the act of assigning the first value to a declared variable. If you declare a variable but do not initialize it, its value will typically be undefined or garbage (contains whatever was in that memory location previously). Basic Things to keep in mind while creating Variables in PHP: – To initialize a variable you just need the assignment operator (=) then write the value of the variable for example $min_age=18 or $name= ”Mohit”. Now we will check this basic example to understand Variables in PHP. The output of the above-written code will be this: “Hello Mohit Your age is 22. Your height is 5.9 and You are from Delhi“. I hope now you would have a clear idea about how to declare and initialize variables in PHP. Please continue with the next tutorials of this series. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Variables in PHP Read More »

Hello World program in PHP

We already learned how to install and start the virtual server on a PC. Here we will check out how to write a simple Hello World program in PHP to display Hello World on the screen. First of all, we will create a folder in the htdocs folder to save our PHP files. I am creating a folder named PHPTutorials. You can see it in the image given below. In this PHPTutorials folder, we will create a file “HelloWorld.php”. It’s important to know that PHP files are saved using the .php extension. Note! You can use any text editor to write PHP code like Notepad, Notepad++, Sublime Text. We will use here Sublime Text. Basic PHP code Syntax: To keep PHP code separate from HTML and CSS we have to write PHP code in Canonical PHP tags, which means we will write the whole PHP code inside these tags. Now we will see an example of the Hello World program. As you can see we have written our PHP code inside Canonical PHP tags which keep it separate from the rest of the code. we have used echo to show output on the screen which is Hello World. Now please save this file as HelloWorld.php in the PHPTutorials folder. After saving it we will run this code in the browser. Note! Please make sure the Apache server is in start mode in your xampp software. To run the code make sure your XAMPP server is on then type this localhost/PHPTutorials/HelloWorld.php chrome address URL bar and hit enter. You will see output like this. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Hello World program in PHP Read More »

Environment Setup For PHP

In this tutorial, we will learn about the steps involved in Environment setup for PHP. As we learned in the last tutorial we have to install a virtual server on our PC to run PHP code. Here we will learn in detail how we can install a virtual server on our PC. Many virtual servers are available in the market, which helps us create PHP projects. But we will use the XAMPP virtual server in this tutorial series. You will be glad to know that the XAMPP server is available for most of the commonly used operating systems such as Windows, Mac and Linux. Please click on the following link that will lead you to the apachefriends.org website from there you can download XAMPP software according to the configuration of your system. Click Here To Download the XAMPP SERVER After the completion of the download and installation process, you can see there is an XAMPP folder in C drive (in Windows) as shown in the picture. Please note if you have choosed differenct location while installing xampp. Then the xampp folder will on the selected location If you open this XAMPP folder then you will see a folder named htdocs there. In this htdocs folder, you have to create a folder for your project and save all files related to the project in this newly created folder. But now the question arises is how to run our project. So to run the project you have to start localhost(a local server). To do this, search XAMPP in your system and open the XAMPP Control panel. A Screen like this will open. just click on the start button in front of Apache and MYSQL as shown in the image below. After that open any browser and type localhost in address bar. If you see a screen similar to the one given below it means your virtual server is ready to work. In the next tutorial, we will learn how to write a Hello World program in PHP. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Environment Setup For PHP Read More »

Introduction to PHP

In this tutorial, we will learn an introduction to PHP. PHP stands for Hypertext preprocessor and it is a well-known language for developing web applications. In short definition, PHP is a server-side, open-source technology used to develop web applications. Web applications help us process data on the server side and generate dynamic web pages for clients. PHP was developed by Rasmus Lerdorf in 1994. Difference between Client-side and server-side scripting language: – You might have heard about client-side or server-side scripting languages. But do you know the difference between client-side and server-side scripting languages? We can not run code written in PHP directly on our PC. So it means we need a server to run code written in PHP. Do not worry I am not gonna tell you to buy a server. We have an alternative solution, we can create a virtual server on a PC. There is a lot of free software available that allows us to create a virtual server on our PC.  Versions of PHP: Before jumping to coding in PHP. Please take a quick glance at the history of the versions in PHP and here you can also see the latest available version. Version Release Date Supported until 7.0 3 December 2015 10 January 2019 7.1 1 December 2016 1 December 2019 7.2 30 November 2017 30 November 2020 7.3 6 December 2018 6 December 2021 7.4 28 November 2019 28 November 2022 8.0 26 November 2020 26 November 2023 8.1 25 November 2021 25 November 2024 8.2 8 December 2022 8 December 2025 8.3 23 November 2023 23 November 2026 Please continue with the upcoming tutorials to learn about how to set the environment for PHP on PC and how to write a Hello World program in PHP. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Introduction to PHP Read More »

Scroll to Top
×