Main article: PHP syntax and semantics
The following Hello world program is written in PHP code embedded in an HTML document:However as PHP does not need to be embedded in HTML, or used with a web server, the simplest version of a Hello World program can be written like this:<!DOCTYPE html> <meta charset="utf-8"> <title>PHP Test</title> <?php echo 'Hello World'; ?>
<?= 'Hello world';
<?php
to open and ?>
to close PHP sections. <script language="php">
and </script>
delimiters are also available, as are the shortened forms <?
or <?=
(which is used to echo back a string or variable) and ?>
as well as ASP-style short forms <%
or <%=
and %>
. While short delimiters are used, they make script files less portable as support for them can be disabled in the PHP configuration, and so they are discouraged.[37] The purpose of all these delimiters is to separate PHP code from non-PHP code, including HTML.[38]The first form of delimiters,
<?php
and ?>
, in XHTML and other XML documents, creates correctly formed XML 'processing instructions'.[39] This means that the resulting mixture of PHP code and other markup in the server-side file is itself well-formed XML.Variables are prefixed with a dollar symbol, and a type does not need to be specified in advance. Unlike function and class names, variable names are case sensitive. Both double-quoted (
""
) and heredoc strings provide the ability to interpolate a variable's value into the string.[40] PHP treats newlines as whitespace in the manner of a free-form language (except when inside string quotes), and statements are terminated by a semicolon.[41] PHP has three types of comment syntax: /* */
marks block and inline comments; //
as well as #
are used for one-line comments.[42] The echo statement is one of several facilities PHP provides to output text, e.g., to a web browser.In terms of keywords and language syntax, PHP is similar to most high level languages that follow the C style syntax.
if
conditions, for
and while
loops, and function returns are similar in syntax to languages such as C, C++, C#, Java and Perl.
No comments:
Post a Comment