PHP BASIC

Definition PHP

PHP is a server-side scripting language, a programming language used to develop static websites or dynamic websites or Web applications. PHP stands for Hypertext Pre-processor, which was previously called Personal Home Pages. As a scripting language, PHP executes programming instructions at runtime. PHP is a server-side programming language, so scripts from PHP will be processed on the server. Server types that are often used in conjunction with PHP include Apache, Nginx, and LiteSpeed. PHP is a generic programming language which means it can be embedded into HTML code, or it can be used in combination with various web template systems, web content management systems, and web frameworks.

PHP Functions

The function of PHP is to create or develop static websites or dynamic websites or Web applications. Although actually PHP is not the only programming language that can be used to load websites. PHP is used because to create dynamic websites it can be used to store data into a database, create pages that can change according to user input, process forms, etc.

PHP Basic Syntax

Text Box: <?php
 echo “Selamat datang”;
 ?>
1. Each programming language has its own coding rules. Likewise with PHP. The basic syntax opens with as shown in the following example:

Here is the explanation of the code:

  • <?php this is a mandatory code to open a PHP program.
  • Echo is a command to display text.
  • “Welcome”; the text to be displayed and written between quotation marks and semicolon.
  • ?> is the code to terminate PHP and must be used when combined with other programming languages such as HTML.

Text Box: <?php
 $alamat = “Yogyakarta”;
 echo $alamat; ?>
2. PHP syntax is case sensitive. So, the use of uppercase or lowercase letters will also affect the output given. As an example :

The code above will produce the output: Yogyakarta on the website page.

Text Box: <?php
 $alamat = “Yogyakarta”;
 echo $Alamat;
 ?>
“>Namun, jika dituliskan seperti ini:</p>



<p>You will get an error display. The reason is that there is a difference between the variables $address and $Address.</p>



<p><strong>Syntax PHP</strong></p>



<p>Understanding PHP Syntax is a writing rule so that it can be understood correctly by the compiler when reading the programming language. In correct PHP writing, it begins with “”. And inside PHP Files can also contain tags like HTML and client side scripts like JavaScript.</p>



<p><img loading=3. In PHP, you can also write comments as an explanation of the code written. Comments in PHP use // or */ and will not appear as output in the browser. Examples are as follows:

All PHP code written must be saved with a .php extension file.

PHP Code Example

Text Box: <!DOCTYPE html> 
<html> 
         <head> 
	  <title>Contoh</title>
         </head>
         <body>
	 <?php 
	          echo "Halo, Selamat Siang";
	  ?>
            </body>
 </html>
1.

  1. HTML
    You can insert PHP code inside HTML using your favorite PHP editor. An example of its use is as follows:

The above code will return the result in the browser:

As seen the PHP code is inserted in the HTML body. With the echo command, the browser returns the commanded text result.

2. CSS

Text Box: <!DOCTYPE html>
<html>
<body>
<head>
           <style>
  h2{  
  color:white;  
  background-color:red;  
  padding:5px;  
  }  
  p{  
  color:blue;  
  }    
 </style>
 <?php 
 echo "<h1>Selamat Datang</h1>";
 echo "<p>Semoga Harimu Menyenangkan</p>";
  ?>
</body>
</html> 
The following is an example of using combined with HTML and CSS:

When used in conjunction with CSS, the output rendered has CSS attributes such as different colors and font sizes as follows:

3. JavaScript

Text Box: <!DOCTYPE html>
<html>
<body>
 <h2>Selamat Datang<br>
 Semoga Harimu Menyenangkan</h2>
 <b>Ini adalah Contoh Gabungan PHP dan Javascript </b>
 <br>
 <p id="demo">PHP Dasar</p>
 <button type="button" >

An example of using combined code with JavaScript is shown in the following code:

Based on the code above, the browser will display the following output:

4. Combine

When used in conjunction with HTML, CSS and JavaScript at the same time, the sample code used is as follows:

Text Box: <!DOCTYPE html>
<html>
<body>
<head>
 <style>
  h2{  
  color:white;  
  background-color:red;  
  padding:5px;  
  }  
  p{  
  color:blue;  
  }    
 </style>
</head>
<h2>Ini merupakan contoh<br>
Penggabungan CSS, HTML, dan Javascript</h2>
<b>Dengan PHP </b>
<br>
<p id="demo">Contoh</p>
<button type="button" >
“></td></tr></tbody></table></figure>



<p><strong>Here’s how it looks in the browser:</strong></p>



<figure class=

PHP Code Writing

1. PHP Native

Native is writing PHP code from scratch when designing a website. PHP Native is often used by developers who have fairly good coding skills or those who want to create unique flow frameworks with high functionality.

2. PHP Framework

When using a framework, developers can take advantage of a ready-made website management framework. That is, there is no need to make it from scratch to make work easier. Frameworks can help developers work more efficiently and complete website development faster.

Some of the popular PHP frameworks used include: CodeIgniter, Laravel, Yii, Symfony and Zend Framework.

PHP and MySQL

PHP is often used in conjunction with MySQL to build dynamic websites. MySQL is a database management system that will access and process data. How do the two work?

Previously PHP and MySQL had to be connected to each other. Well, when there is a request from the browser to the web server, PHP will contact the MySQL server to find the data needed in the database. After getting the data, the MySQL server will forward the information to the server to proceed to the browser.

So, with PHP and its ability to communicate with a database, it will be easier for you if you want to develop a bigger website.

PHP File Extension

PHP has its own extension, the function of the extension is different so that the server can identify PHP files and scripts. PHP files must be saved with the “.php” extension. Older PHP file extensions include

  1. .phtml
  2. .php3
  3. .php4
  4. .php5
  5. .phps

Why use PHP?

Below are some strong reasons why many people prefer PHP and make PHP the most popular programming language:

  1. PHP is a language that is open source and free.
  2. The process of learning PHP programming is shorter than other languages ​​such as JSP, ASP etc.
  3. Because the most popular programming language, the community has a lot of documentation is bigger
  4. Most web hosting servers support PHP by default unlike other languages ​​like ASP which require IIS. This makes PHP a cost-effective choice
  5. PHP is updated regularly to keep up with the latest technology trends.
  6. PHP is a server-side scripting language; which means it only needs to install it on the server and client computers requesting resources from the server don’t need to install PHP; web browser only
  7. PHP works with MySQL ; This does not mean that you can use PHP with other database management systems. You can still use PHP with Postgres, Forecaster, MS SQL Server ODBC etc.
  8. PHP is a cross-platform language; which means it can be applied to a number of different operating systems like windows, Linux, Mac O etc.

Must-know PHP Security

  1. Use htmlspecialchars

To be able to see what scripts are looking for and avoid Cross-Site Scripting (XSS) , use htmlspecialchars in your PHP code. This will help you convert characters in PHP into HTML form. For example:

& (sign “and” or ampersand ) → & amp;
”(double quotes or double quotes ) → & quot;
‘(single quote ) → & # 039;
<(less than or less than sign ) → & lt;

(greater than or greater than sign) → & gt;

2. Save Session with IP

To prevent session hijacking, you should always save sessions with a special IP address that you use frequently. The trick, add this code into your script.

$IP = getenv ( “REMOTE_ADDR” );

This method can help you to cancel the session whenever there is a suspicious request and without your knowledge. The code above will also provide information that someone is trying to access your session.

3. Use PHP Data Objects (PDO)

To avoid SQL injection attacks, you should use PHP Data Objects or PDO. PDO is an add-on that helps you access databases securely. It doesn’t matter what kind of database you use.

However, this PDO cannot be used to edit SQL or add certain features. But precisely because of the attack, you can avoid SQL attacks.

4. Files from Browser

PHP frameworks that use the MVC (Model View Controller) architecture, Laravel or CodeIgniter for example, have a structured file directory view. Not all files will be placed by the browser. However, the file can still be viewed.

Here it is, misery. Most of the lay developers put all the website files in the root directory. This makes it easier for hackers to break into important website data. Because the browser can see all these files, it will be easy for hackers to insert malicious code/scripts into your site

Document Root Settings

The document root for a PHP application on any server should be set into the /var/www/html directory so that users can access your website via a browser. But in some cases, when you develop API with frameworks like Laravel and CodeIgniter you need to update webroot to /public folder.

The purpose of helping webroot to var / www / html / public is to populate sensitive files like .htaccess and .env which contain environment variables, database access, email, payment API, etc.

Always use SSL

All modern web browsers such as Google Chrome, Firefox, Opera, and others, always use the HTTPS protocol for websites. Because HTTPS provides a secure and encrypted access protocol for untrusted sites.

SSL certificates also protect your website against XSS attacks and prevent hackers from entering code into your website. To get an SSL certificate you can follow the guide on the free ssl article on our blog.

7. Update PHP regularly

Currently, the most stable and latest PHP version available is PHP 7.4.5. I need to update your website to use the latest PHP version.

If you are still using PHP 5.6 then you will have a lot of problems updating PHP. Because you also need to update your website code and change the logic of functions like password hashing and others.

If you are using PHPStorm then you can use PHP 7 Compatibility Inspection, which will show you which code is causing the Error.

The need for digital IT is needed in daily activities, Bead IT Consultant is the right choice as your partner, visit our website by clicking this link: www.beadgroup.com