PHP Tutorial for Beginners | Learn PHP | Part-1

PHP Tutorial for Beginners | Learn PHP | Part-1

14 June 2023 1 By Anshul Pal

In this PHP tutorial we are going to learn PHP from the beginning. Before learning PHP,  it is mandatory that you should have a basic understanding of HTML,CSS and JAVASCRIPT.

Points to be covered in this tutorial

  1. Introduction of PHP
  2. How to Install and  Run PHP code in your Machine
  3. Syntax of PHP
  4. Variables in PHP

Introduction to PHP

PHP is an open source server side scripting language. It stands for Hypertext Preprocessor. PHP scripts are executed on the server and free to use. It is used by many developers for web development. More than 80% of websites are made with the help of PHP. It is a very popular serverside scripting language. PHP is the first choice of web developers for backend.

PHP is mostly used for making web servers. It runs on the browser and is also capable of running in the command line. So, if you don’t feel like showing your code output in the browser, you can show it in the terminal. PHP makes webpages dynamic page content. It can send and receive cookies and is capable of adding, deleting and modifying data in your database and supports a wide range of databases. It runs on various platforms, such as Linux, Unix, Windows,  Mac OS etc.

  • PHP was created by  Rasmus Lerdorf in 1994 and is currently managed by a PHP development team.
  • PHP files  can contain text, HTML, CSS, JavaScript, and PHP code.
  • PHP files are saved in the “.php” extension.

How to Install and  Run PHP code in your Machine

Method 1 –  Use XAMPP

I personally suggest you download XAMPP Server to your PC. XAMPP server includes an Apache web server and the mysql database. After that, you have to download a PHP parser to generate HTML output that can be sent to the Web Browser.

Method 2 –  How to install and run PHP in Windows

So if you want only PHP installation (withut apache server), please follow these steps for installation:

Download PHP 8 – Similarly, depending on your system build download the Thread Safe version of PHP.

image2

Note- Please Download and install latest 14.31.31103.0 Visual C++ Redistributable Visual Studio 2015-2022 : vc_redist_x64 or vc_redist_x86 software.

Now we install PHP 8. To do that, similarly, move the downloaded and extracted PHP 8 binary files to the C drive. In my case, I renamed php-8.2.4 to php folder.

Now Copy the PHP 8 home path i.e C:\php and set it into your machine environment variable just like above “apache setup”.

image 4

Open the command prompt and type php -v to check whether the path is set correctly or not. If the PHP path is set correctly, it will print the PHP version.

image 6

Go to the PHP home directory C:\php, and you will find two configuration files php.ini-development and php.ini-production. Create a copy of php.ini-development and rename it to php.ini

image 7

How to run PHP Code via inbuilt PHP server

PHP provides a built-in web server, which you can launch by navigating to a folder and running the PHP executable with an -S parameter to set the localhost port. For example:

cd myproject //folder name where your php files stored
php -S localhost:8000

You can then view PHP pages in a browser at http://localhost:8000.

This may be adequate for quick tests, but your live server will use Apache or similar web server software. Emulating that environment as closely as possible permits more advanced customization and should prevent development errors.

Use PHP Interactive Shell

Just type php -a on your command prompt.

php shell

Method 3 – Set up Web Server on your own PC (Manually)

If you want install and configure manually, Please visit this article : How to install Apache 2.4 and PHP 8 on a Windows

PHP Syntax

PHP or any programming language has a way to write its code and that way is known as syntax. If you have a better understanding of its syntax, that makes your foundation strong. The PHP script is executed in the browser and, as a result, it generates an HTML page on the browser. A PHP file contains HTML tags and some PHP scripting code and is saved by the .php extension.

<?php
//Here you write the php code
?>

Let us see an example.

<! Doctype html>
<html>
<body>
<h1>Welcome to PHP Tutorial</h1>
<?php
echo "Hello PHP";
?>
</body>
</html>

Here we can see that a php code starts with a <?php tag and then we use the keyword echo to print the given statement. After that, we close our PHP tag by ?> and all PHP code comes under HTML.

  • PHP statements ends with a semicolon ( ; ).
  • All PHP files are saved with the .php extension.
  • In PHP keywords, classes, functions, and user-defined functions are not case sensitive.
  • All variable names are case sensitive.

Variables in PHP

As we discussed about, PHP variables are case sensitive and in PHP we write variable names with ( $ ) sign.

Rules for PHP variables.

  • In PHP, a variable name always starts with the ( $ ) sign.
  • Variables used before they are assigned have default values.
  • Variable names after the $ sign must start with the letter or underscore.
  • A variable name cannot start with a number or any special character, rather than underscore character.
  • In PHP, variable names are case sensitive ($name and $NAME are two different variables.
  • PHP is a loosely typed language and we do not require to declare the data types of variables, rather PHP assumes it automatically by analyzing the values.

Creating PHP Variables

Example

<?php
$num = 56;
$num2 = 125.56;
$txt = "Hello World" ;
?>

In the above example, we are only declaring the value to be variable. As you can see, all variable names start with a $ sign and all variables have taken different-different types of values and we don’t have any need to mention the data type for that.

Output Variable

PHP has two basic ways to get output on the screen.

  1. Echo statement
  2. Print statement

Note :- The difference between echo and print is echo has no return value, while print has a return value ( 1 ) and by using the print we cannot pass multiple arguments.

Echo Statement

We can use echo statement with or without parentheses ().

Example

<?php

echo “<h2>I am new at PHP</h2>”;

echo “PHP is awesome<br>”;

echo “this “, “is “, “example “, “for “, “made “, “string “, “with “, “multiple parameters. “;

?>

Output

PHP image

Print Statement

Print statement can be used with or without parentheses similarly as echo statement and it is slower than echo statement.

Example

<?php
$txt1 = "Welcome To PHP Tutorial Dude";
$txt2 = "APalgorithm.com";
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
?>

Output

Php example

In the above example, we are taking two variables, $txt1 and $txt2. After that, we use a print statement with a heading tag and dot operator. Similarly, we are printing our second variable and then you can see the output.

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.