3 Ways to Write PHP Code

Table of contents:

3 Ways to Write PHP Code
3 Ways to Write PHP Code

Video: 3 Ways to Write PHP Code

Video: 3 Ways to Write PHP Code
Video: Find Your HP Laptop Name, Product Number, or Serial Number | HP Notebooks | HP Support 2024, May
Anonim

PHP is a server scripting language used to create interactive web pages. This language has become popular because of its ease of use, interaction within web pages, and its integration with HTML. Think about what happens when a page on a website is edited. Behind the process, there is a lot (perhaps hundreds) of PHP code that controls web page changes based on various situations/conditions. This wikiHow teaches you how to write some simple PHP code so you can understand the basics of how PHP works.

Step

Method 1 of 3: Starting with an “Echo” Statement

Write PHP Scripts Step 1
Write PHP Scripts Step 1

Step 1. Open a text editing program

You will use this program to write and edit PHP code.

  • Notepad can be accessed in various versions of Windows via the shortcut Win + R. After that, type “Notepad”.
  • TextEdit can be used on Mac computers by going to the “Applications” > “TextEdit” folder.
Write PHP Scripts Step 2
Write PHP Scripts Step 2

Step 2. Type a simple statement into the Notepad window

Segments in PHP code begin and end with PHP markers enclosed in angle brackets (“”). “Echo” is a very basic statement (command for computer) in PHP language that will display text on the screen. The text you want to display must be enclosed in quotation marks and end with a semicolon.

The code will look like this:

Write PHP Scripts Step 3
Write PHP Scripts Step 3

Step 3. Save the file with the name “helloteman” and the extension.php

You can save it by opening the “File” menu > “Save As…”.

  • In Notepad, insert the.php extension at the end of the filename and enclose it in quotation marks. This will prevent the file from being converted to a plain text file by Notepad. Without the quotes, the file will be saved with the name “hello friends.php.txt”. Alternatively, you can select the drop-down menu in the “Save as type” section and change it to “All Files (*.*) " so that the file can be saved in the name you typed, and you don't need to insert quotes.
  • In TextEdit, you don't need to insert quotes, but a pop-up window will appear asking you to make sure that the file really needs to be saved with a.php extension.
  • Make sure you save the file to the "server" document root directory. Usually, this directory is a folder called “htdocs” in the “Apache” folder on Windows, or “/Library/Webserver/Documents” on a Mac. However, the main folder can be set or selected manually by the user.
Write PHP Scripts Step 4
Write PHP Scripts Step 4

Step 4. Access the PHP files via a web browser

Open the desired web browser and type the following address into the address bar using the name of the saved PHP file: https://localhost/halotemanteman.php. The browser window will display an “echo” statement after it.

  • If you receive an error message, make sure you typed in the exact code shown above, including the colon symbol.
  • Also, make sure the file is saved to the right directory.

Method 2 of 3: Using PHP and HTML

Write PHP Scripts Step 5
Write PHP Scripts Step 5

Step 1. Understand the "php" tag

The “” flag tells the PHP engine that the entry or element added in between is PHP code. Entries or elements outside of these markers will be treated as HTML and ignored by the PHP engine so that they are sent to the browser just like any other HTML code or element. The important thing that you should know is that the PHP script or code is embedded inside a plain HTML page.

Write PHP Scripts Step 6
Write PHP Scripts Step 6

Step 2. Understand the statements added between the markers

Statements are used to instruct the PHP engine to do something. In an “echo” statement, for example, you can tell the machine to print or display what is enclosed in quotes.

The PHP engine itself doesn't actually actually print anything to the screen. The output generated by the engine is sent to the browser as HTML code. The browser does not "know" that the received element or code is PHP output. What the browser understands is that the input it receives is plain HTML code

Write PHP Scripts Step 7
Write PHP Scripts Step 7

Step 3. Use HTML markers to emphasize statements

Adding HTML markers can change the output of PHP statements. markers ” “ ” function applies bold formatting to text that is added between the two. Keep in mind that this marker is added outside the text that needs to be bolded, but inside the quotation marks of the “echo” statement.

  • Your code will look like this:

    <?php?

    echo Hello friends!

    ";

    ?>

Write PHP Scripts Step 8
Write PHP Scripts Step 8

Step 4. Save and open the file in the browser

Select the menu “File” > “Save As…” and save the file as “haloteman2.php”, then open it in a browser with the following address: https://localhost/haloteman2.php. The output code will be the same as the previous code, but this time text is displayed in bold.

Make sure you save the file to the main “server” document directory. Usually, this directory is a folder called “htdocs” in the “Apache” folder on Windows, or “/Library/Webserver/Documents” on a Mac. However, the main folder can be set or selected manually by the user

Write PHP Scripts Step 9
Write PHP Scripts Step 9

Step 5. Edit the file to add a second “echo” statement

Keep in mind that each statement needs to be separated by a semicolon.

  • Your code will look like this:

    <?php

    echo “Hello, guys!”

    ;

    echo "How are you?";

    ?>

Write PHP Scripts Step 10
Write PHP Scripts Step 10

Step 6. Save and run the file as " hellofrienddobel.php"

The page will display two “echo” statements displayed sequentially in two lines of text. Pay attention to the code"

” on the first line. The code is an HTML marker to insert a new line.

  • If you don't add it, the code output will look like this:

    Hello guys! How are you?

Method 3 of 3: Recognizing Variables

Write PHP Scripts Step 11
Write PHP Scripts Step 11

Step 1. Think of variables as “containers” of data

To manipulate data, both numbers and names, you need to store it in a “container”. This process is known as variable statement. The syntax for declaring a variable is “$MyVariable = “Hello, friends!”;”

  • The dollar ($) symbol at the beginning of the code tells PHP that “$MyVariable” is a variable. All variables must begin with a dollar symbol, but you can name variables with any name.
  • In the example above, the existing data is "Hello, guys!" and the variable used is "$Variableku". You're telling PHP to store the data on the right side of the equals symbol into a variable on the left side of the equals symbol.
  • Variables containing text data are known as strings.
Write PHP Scripts Step 12
Write PHP Scripts Step 12

Step 2. Name an existing variable

The use or reference of variables in code is known as "call" or "call". Declare your variables, then use or “call” them instead of manually typing in text data.

  • Your code will look like this:

    $My Variable = “Hello, guys!”;

    echo $myvariable;

    ?>

Write PHP Scripts Step 13
Write PHP Scripts Step 13

Step 3. Save and run the file

Go to the “File” menu > “Save As…” and save the file with the name “myfirstvariable.php”. Open a browser and visit https://localhost/mypertamavariabel.php. After that, the script or code will display the variable in the browser window. The output will be the same as plain/plain text, but the display process or appearance is different.

Make sure you save the file to the main “server” document directory. Usually, this directory is a folder named “htdocs” in the “Apache” folder on Windows, or “/Library/Webserver/Documents” on MacOS X. However, the main folder can be set or selected manually by the user

Write PHP Scripts Step 14
Write PHP Scripts Step 14

Step 4. Use variables with numbers

Variables can also contain numbers (known as integers), and these numbers can be manipulated using basic mathematical functions. Start by creating three variables named “$SmallNumber”, “$LargeNumber”, and “$Amount”.

  • Your code will look like this:

    <?php

    $SmallNumbers;

    $BigNumber;

    $Amount;

    ?>

Write PHP Scripts Step 15
Write PHP Scripts Step 15

Step 5. Assign a value or number data to the first two variables

Enter the number data into the variables "$SmallNumbers" and "$BigNumbers".

  • Keep in mind that number data does not need to be enclosed in quotation marks. If enclosed, numbers will actually be considered as text data such as the variable "Hello, friends!".
  • Your code will look like this:

    <?php

    $LiteNumber = 12;

    $BigNumber = 356;

    $Amount;

    ?>

Write PHP Scripts Step 16
Write PHP Scripts Step 16

Step 6. Use the third variable to calculate and display the sum of the other variables

Instead of calculating yourself, you can name the first two variables in the “$Amount” variable. With a mathematical function, the machine will calculate the sum of the two variables by itself. To display the result, you just need to add an “echo” statement that returns the variable after the declaration.

  • Changes to the number data will take effect when you display the variable “$Amount” with the statement “echo”.
  • Your code will look like this:

    <?php

    $LiteNumber = 12;

    $BigNumber = 356;

    $Sum = $SmallNumbers + $BigNumbers;

    echo $Amount;

    ?>

Write PHP Scripts Step 17
Write PHP Scripts Step 17

Step 7. Save the file and run the script or code

The browser window will display only one number. The number is the result of the sum of the two variables mentioned in the variable "$Amount".

Write PHP Scripts Step 18
Write PHP Scripts Step 18

Step 8. Review the text variable (string)

By using variables to store text data, you can specify these variables whenever you want to use text data so you don't have to always type in text data manually. This procedure also makes it easier for you to perform more complex data manipulation in the future.

  • The first variable, “$MyVariable” contains text data or the string "Hello, friends!". The variable will always contain the text “Hello, guys!” unless you change the text.
  • The statement “echo” will display text data stored in the variable “$MyVariable”.
Write PHP Scripts Step 19
Write PHP Scripts Step 19

Step 9. Review number or integer variables

You've covered the basics of manipulating number variables using mathematical functions. Data from mathematical calculations can be saved to other variables. This is only the beginning of the various results that can be obtained through the variables you create.

  • Both the variable "$SmallNumbers" and "$BigNumbers" are added with number data.
  • The third variable, “$Amount” stores the sum of “$SmallNumbers” and “$LargeNumbers”. Since the variable “$SmallNumber” stores the first data and “$BigNumber” stores the second data, the variable “$Sum” contains the data for adding the first number to the second number. Data or values can be changed through changes in one of the variables used.

Tips

  • For this article, it is assumed that you have Apache and PHP installed on your computer. Whenever you are prompted to save a file, you will need to save it in the " \ht docs " (Windows) or “\Library\WebServer\Documents” (Mac) directory, in the Apache directory.
  • Comments are an important element of any programming so make sure you know how to add comments in PHP.
  • A very useful tool for testing PHP files is XAMPP, a free program that installs and runs Apache and PHP, and helps you simulate a server on your computer.

Recommended: