search
HomeBackend DevelopmentPHP TutorialAn easy way to embed PHP in HTML

An easy way to embed PHP in HTML

Apr 16, 2018 pm 05:18 PM
htmlphpmethod

This article mainly introduces in detail the simple method of embedding PHP in HTML. It uses an example to embed PHP into HTML. Interested friends can refer to it.

We submit an order with one And the example of displaying order information is the beginning of learning PHP. This example contains two files. An html file for submitting orders: orderform.html, and a php file for displaying order information: processorder.php. I put these two files under the test_1 folder and the test_1 folder under the htdocs directory.
The organization of the file is as shown in the figure below, using the integrated environment installed by xampps.

The html file orderform.html for submitting the order is as follows:

<form action="processorder.php" method="post">
  <table>
    <tr bgcolor="#cccccc">
      <td width="150">Item</td>
      <td width="15">Quantity</td>
    </tr>
    <tr>
      <td>Tires</td>
      <td align="center"><input type="text" name="tireqty" size="3" maxlength="3" /></td>
    </tr>
    <tr>
      <td>Oil</td>
      <td align="center"><input type="text" name="oilqty" size="3" maxlength="3" /></td>
    </tr>
    <tr>
      <td>Spark Plugs</td>
      <td align="center"><input type="text" name="sparkqty" size="3" maxlength="3" /></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><input type="submit" value="Submit Order" /></td>
    </tr>
  </table>
</form>

Display order information The php file processorder.php is as follows:

<?php
// create short variable names, also can use &#39;$_REQUEST[&#39;name&#39;]&#39;
$tireqty = $_POST[&#39;tireqty&#39;];
$oilqty = $_POST[&#39;oilqty&#39;];
$sparkqty = $_POST[&#39;sparkqty&#39;];
?>

<!DOCTYPE html>
<html>
<head>
  <title>Bob &#39;s Auto Parts - Order Results</title>
</head>
<body>
  <h1 id="Bob-nbsp-s-nbsp-Auto-nbsp-Parts">Bob &#39;s Auto Parts</h1>
  <h2 id="Order-nbsp-Results">Order Results</h2>
  <?php
  echo "<p>Order processed at ";
  echo date(&#39;H:i, jS F Y&#39;)."</p>";
  echo "<p>Your order is as follows: </p>";
  echo "$tireqty tires<br />";
  echo $oilqty.&#39; bottles of oil<br />&#39;;
  echo $sparkqty." spark plugs<br />"
  ?>
  ---------------------------------------------------<br />
  <?php
  $testHeredoc = <<< EOF
  line 1  
  line 2  
  line 3  
EOF;
  echo "$testHeredoc"."<br />";
  ?>
  ---------------------------------------------------<br />
  <?php
  echo "About Comment:";
  //Here is a comment.
  #Here is a comment too.
  /*
  Here is multi line comment.
  Here is multi line comment.
   */
  ?>
</body>
</html>

Enter http://localhost/test_1/orderform.html in the browser, and the order information will be displayed. Page, as shown below:

#Fill in the number, and then click the "Submit Order" button to submit the content. The page will display the html page generated by processorder.php after being parsed by the PHP parser, as shown below:

In this example, we can learn the following points :

1. The syntax format for embedding PHP code in HTML is: . It should be noted that there cannot be a space between the start symbol "

2. The form content submitted by the post method can be obtained by name through the "$_POST[]" array of PHP, or through the "$_REQUEST[]" array. These arrays are super global variables.

3. Strings can be enclosed in single quotes or double quotes, or they can be enclosed in back-single quotes (the back-single quotes are in the upper left corner of the keyboard and are the same key as ~).

Three types of quotation marks have different effects:

  • The string within single quotation marks will be output as plain text;

  • If there is a variable in the double quotes, it will be replaced with the value of the variable and the text will be output;

  • Anti-single quotes are called executors. The PHP parser will first execute the content in the anti-single quotes and return the result after execution.

#4. Strings can be connected together using the period "." The dot is the only string concatenation character in PHP, which is equivalent to " " in Java.

5. There are three comment methods in php: Java-like single-line comments "//"; shell-like single-line comments "#"; Java-like multi-line comments "/**/".

6.php All variables start with "$" when used, and variables do not need to be declared in advance when used.

And the type of a variable can also change at any time, depending on the type of value assigned to the variable. The type of a PHP variable is determined and changed with each assignment.
That’s it for the first php example. I hope you will continue to pay attention to the articles compiled by the editor.


The above is the detailed content of An easy way to embed PHP in HTML. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.