My First PHP Code

In this Lesson we’ll talking about how to create a PHP File and how to write PHP code. Basically in this lesson we’ll learn about PHP Syntax.

Step – 1

First open your htdocs folder. But where is the htdocs folder ?

You will find the htdocs folder in your xampp folder.

How to open XAMPP folder easily ?

Open your XAMPP Control Panel and then click on Explorer

Xampp control panel
Xampp control panel

Step – 2

Inside htdocs folder create a new folder and name it whatever you want, here I named it php_tutorial.

After that go inside the newly created folder (php_tutorial) and create a new file called index.php

index.php is a PHP file. Note: Every PHP files extension is .php

Now open index.php file in your code editor to write PHP Code. you can use Notepad ++, Brackets, VS code editor for coding.

Important

  • PHP code start with <?php
  • PHP code end with ?>
<?php
// PHP code goes here
?>

PHP statements end with a semicolon ;

<?php
echo "This is a Statement";
?>

PHP Comments

A comment in PHP is used to describe a code or programme. This will help other developers to understand your code easily and also help you because sometimes you forget why you wrote this code or programme.

And yes comments are not executed. That means you can’t see comments in browsers.

In PHP you can see two types of comments.

  • Single line comments
  • Multi line comments

How to add comments in PHP ?

<?php
// this is single line comment
# this is also a single line comment
/*
Hi,
This is multi line comment
*/
echo "You can't see comments in browsers";
?>
Browser Output
You can’t see comments in browsers

PHP Case Sensitivity

PHP is partially case sensitive. Because there is something case-sensitive in PHP and some are case-insensitive.

Case Sensitive( For both user-defined and php-defined )

  • Variables
  • Constants
  • Array keys
  • Class properties
  • Class constants

Example:

<?php
// Variable example
$name = "John";
echo $name; // This is correct
echo $Name; // This is incorrect
echo $nAme; // this is incorrect

// Constant example
define("FULL_NAME", "John Doe");
echo FULL_NAME; // This is correct
echo Full_Name; // This is incorrect
echo full_name; // This is incorrect
?>

Case Insensitive ( For both user-defined and php-defined )

  • Functions
  • Keywords (if, else, echo, while, for etc.)
  • Class constructors
  • Class methods

Example:

<?php
// Function example
function say_hi(){
  echo "Hi";
}
say_hi(); // this is correct
say_Hi(); // this is also correct
SaY_hI(); // this is also correct

// Keyword example
// this is correct
if(10 > 5){
  echo "this is Right";
}
else{
  echo "this is wrong";
}
// this is also correct
iF(10 > 5){
  EcHo "this is Right";
}
eLsE{
  eChO "this is wrong";
}
?>

How to Run PHP code in your browser ?

Open your Xampp control panel and start Apache server.

Start apache server

After that open your browser and type on URL –
http://localhost/php_tutorial/index.php

Note: php_tutorial is the folder that I created inside the htdocs folder.

If you have given that folder another name then you type in your browser
http://localhost/your_folder_name/index.php

After that you can see a blank page on your browser, because we do not write any PHP code.


Step – 3

Let’s start to write PHP Code. So open your index.php file in your editor.

echo is used to show something in output.

<?php
// echo is used to show something in output.
echo "Hi, I'm PHP";
?>
Browser Output
Hi, I’m PHP

You can write PHP code anywhere in a PHP file and you can also wirte HTML, CSS and JS in your PHP file.

Example:

<!DOCTYPE html>
<html>
<body>
<h1><?php echo "Hello PHP"; ?></h1>
</body>
</html>
Browser Output
Hello PHP

Posts Navigation


Leave a Reply

Your email address will not be published. Required fields are marked *