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
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.
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.
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
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
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
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:
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
Why use PHP? Below are some strong reasons why many people prefer PHP and make PHP the most popular programming language:
Must-know PHP Security
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;
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 |