Transcript for:
Introduction to PHP Basics and Syntax

PHP, a server-side scripting language used to create dynamic websites. Despite claims that it's been considered dead for a decade, PHP is still used in almost 78% of all websites including some popular ones like Facebook, Wikipedia, and Flipkart. What a joke!

Originally, PHP was an abbreviation for Personal Home Page. But now it is a recursive acronym for PHP Hypertext Preprocessor. Well, this is confusing. I prefer the earlier one. Anyway, PHP development began in 1993 when Rasmus Laerdorf created programs in C to manage his personal website and the rest is history.

You can directly write PHP code inside your HTML file and while doing that change your HTML file extension from.html to.php. But this creates a problem as we can see raw HTML inside the web browser. How to fix this?

Well, as I told you earlier, PHP is a server-side scripting language. So, we need a server that will execute our PHP file and then show result in our web browser. For this, we can use XAMPP which can be installed on all major operating systems including my favorite Linux operating systems. So download and install it. After that, you get this control panel which is very important because it is a control panel.

Since we will be only using PHP, click on this start button which will start our Apache server. go to the folder where you have installed xampp then inside htdocs create a new folder for all your code and open your favorite text editor in that folder to start writing now to view your file in the web browser go to localhost slash your folder name then your file name and you will see your file it is blank because i have not written anything so let's write our first php program to write php code inside html type angle bracket question mark php and then at the end question mark and angle bracket inside this we can write our php program like echo php is great just like c or c plus plus we use a semicolon at the end of the line now we see php is great in our web browser in php we use echo to display output there are some other functions too but echo is most commonly used now to write comments in php we can use the same syntax as c or c plus plus that is using two forward slashes or you can also use the hash symbol. For multi-line comments, we can use the same forward slash, star, then at the end, star, forward slash. Commenting in PHP is the same as in C or C++ language but declaring variables is not. This is the syntax to declare variables.

$VARIABLENAME equals value. Every variable name must start with a dollar sign like this variable, $LANGUAGE which stores a string. We can see the value of it in our web browser using echo.

PHP is weird and that is because these variables are case sensitive like these two are completely different. Because the second variable has the letter L capital and it makes sense. But except variables everything else is case insensitive.

Which means no matter if I write echo with a capital H or all capital letters. It will mean the same and everything will work completely fine. So except variables.

everything else is case insensitive which include function names, class names and even PHP keywords. Speaking of keywords, PHP has more than 80 keywords and there are 8 primary data types which include integer which stores whole numbers, then float which stores decimal numbers. After that we have strings which are used to store a sequence of character. You can store a string in either double or single quotes.

Then we have a boolean which represents true or false. We see 1 as an output because true is represented by 1. Then we have arrays which stores a group of values. Unlike in C where we have an array of the same data type, in PHP, an array can store values of different data types. Like this array is valid in PHP.

We can use the array construct to create an array with values separated by commas. To print elements of an array, we can use printr function in PHP. There are two types of arrays in PHP. indexed array and associative array. This is an indexed array.

In this, every value has an index starting from 0. To access the first element, we type the array name and in square brackets, the index of that element. For the first element, it will be 0. In an associative array, we have key value pairs. First, we write the key, then this arrow and then the value.

These key value pairs are separated by commas. To get an individual value, we use keys. instead of index numbers in associative arrays. These keys can be anything from strings to negative numbers and even done null- And the funny thing is that we can use null as a key to access that value. Yup, PHP is weird.

Anyway, moving on to the next data type that is objects. You'd be surprised to know that PHP also supports object-oriented programming. We can define a class using the class keyword and then create an object of that class using the new keyword and then the class name.

To access the element of the class, we can use this arrow symbol and then the element name. After objects, we have resource. which is a special data type in PHP that refers to an external resource like a file, image etc.

For example, here I am opening the hi.txt file using the fopen function and saving it in the file handle variable. Now the data type of this file handle variable is a resource. We can check if a variable is a resource or not using the isResource function. So here, if it is a resource, it will be true and our if condition will run.

Yep, it is a resource. The next and last data type in PHP is NULL. If you assign a NULL value to a variable, then the data type of that variable is NULL.

Same as the isResource function, we can use isNull function to check if the variable is of the NULL data type or not. And same as these two, there are functions to check for other data types. Like isInit function checks if the given variable is of the integer data type, isFloat for the float data type, isString for string, isBool for boolean.

isArray for an array and isObject to check for an object. Now, let's see some commonly used control statements, starting with the if statement. The syntax is similar to C language, first we write if, then the condition and in curly braces the code to run if the condition is true. We can also add else which will run when the condition is false.

Let's make Gemini human and now the code in this else statement has been executed. Next we have the switch statement. which is used to compare the value of a variable with multiple values. First we write switch then the variable name whose value we will be comparing and then different values to compare.

Like if the value of number is 1, this code will execute and then at last a break statement to break out of the switch statement. We can add multiple cases and also a default case which will be executed when no other cases match. Here the value is 1 so the code in case 1 will be executed which is to print 1 If I change the value to 2, the code in case 2 will be executed.

And if I change it to something else rather than 1 or 2, then it won't match with either of these and the default case will be executed. Then we have the while loop, which runs the given code as long as the condition is true. Writing a while loop is so simple. Just write while, then the condition, and then the code to run in curly braces. We see 1 to 10 printed because after the value of the number goes above 10, this less than or equal to condition becomes false and the while loop ends.

After this we have the brother of the while loop which is the do while loop. It works almost the same as the while loop. The only difference here is that it runs the given code and checks the condition. If the condition is not true it won't run again.

Like here if I do 11 then also you can see that 11 is displayed in our browser. That is because this part of the code executed which displayed 11 and then the condition was checked it was false So this do while loop ended. Next, we have the for loop.

We use a for loop when we know how many times we want to run a code. For example, if I want to print hello four times, I can use a for loop like this. First, we write for, then inside these brackets, declare a variable that will keep the count of how many times our for loop is executed. Then we will have a condition. We want to print four times, so I will do i less than or equal to four.

which means this for loop will run until i is less than or equal to 4. After this after every loop i will increase the value of i by 1 using this i++ statement and inside curly braces the code that i want to run and we see hello printed four times. Then we have the brother of the for loop which is the for each loop which is used to easily loop over arrays. This is my array and if i want to print all the values of this array i can use a for each loop.

Then the array name as any name you want to give for example value and in the body echo value. This will print all the values of the array. Here every value of the array is treated as the value variable.

If I change this to any other random name like xoxo then every value of that array will be treated as xoxo variable and our output will be same. What's remaining? Oh yeah functions. To define a function we use the function keyword.

then the function name and in brackets if it takes any parameters. You can leave it empty too and then the code in curly braces. To call the function, simply write the function name and parenthesis and we see by. We can also pass the value to this function like a name and then we will display by and the given name.

So now when calling the function we need to pass the value of the name too like this. We can directly pass the value and it will work perfectly fine. So that's it for PHP in 10 minutes.