Php run php page

# Getting started with PHP

PHP can be used to add content to HTML files. While HTML is processed directly by a web browser, PHP scripts are executed by a web server and the resulting HTML is sent to the browser.

The following HTML markup contains a PHP statement that will add Hello World! to the output:

DOCTYPE html> html> head> title>PHP!title> head> body> p> echo "Hello world!"; ?>p> body> html> 

When this is saved as a PHP script and executed by a web server, the following HTML will be sent to the user’s browser:

!DOCTYPE html> html> head> title>PHP!/title> /head> body> p>Hello world!/p> /body> /html> 

echo also has a shortcut syntax, which lets you immediately print a value. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag

(opens new window) configuration setting enabled.

For example, consider the following code:

Its output is identical to the output of the following:

In real-world applications, all data output by PHP to an HTML page should be properly escaped to prevent XSS (Cross-site scripting

(opens new window) ) attacks or text corruption.

(opens new window) , which describes best practices, including the proper use of short tags ( ).

# Hello, World!

The most widely used language construct to print output in PHP is echo :

Alternatively, you can also use print :

Both statements perform the same function, with minor differences:

  • echo has a void return, whereas print returns an int with a value of 1
  • echo can take multiple arguments (without parentheses only), whereas print only takes one argument
  • echo is slightly faster

Both echo and print are language constructs, not functions. That means they do not require parentheses around their arguments. For cosmetic consistency with functions, parentheses can be included. Extensive examples of the use of echo and print are available elsewhere

C-style printf and related functions are available as well, as in the following example:

(opens new window) for a comprehensive introduction of outputting variables in PHP.

# Non-HTML output from web server

In some cases, when working with a web server, overriding the web server’s default content type may be required. There may be cases where you need to send data as plain text , JSON , or XML , for example.

(opens new window) function can send a raw HTTP header. You can add the Content-Type header to notify the browser of the content we are sending.

Consider the following code, where we set Content-Type as text/plain :

header("Content-Type: text/plain"); echo "Hello World"; 

This will produce a plain text document with the following content:

(opens new window) content, use the application/json content type instead:

header("Content-Type: application/json"); // Create a PHP data array. $data = ["response" => "Hello World"]; // json_encode will convert it to a valid JSON string. echo json_encode($data); 

This will produce a document of type application/json with the following content:

Note that the header() function must be called before PHP produces any output, or the web server will have already sent headers for the response. So, consider the following code:

// Error: We cannot send any output before the headers echo "Hello"; // All headers must be sent before ANY PHP output header("Content-Type: text/plain"); echo "World"; 

This will produce a warning:

Warning: Cannot modify header information — headers already sent by (output started at /dir/example.php:2) in /dir/example.php on line 3

When using header() , its output needs to be the first byte that’s sent from the server. For this reason it’s important to not have empty lines or spaces in the beginning of the file before the PHP opening tag

(opens new window) ) to omit the PHP closing tag ?> from files that contain only PHP and from blocks of PHP code at the very end of a file.

(opens new window) to learn how to ‘catch’ your content into a variable to output later, for example, after outputting headers.

# PHP built-in server

PHP 5.4+ comes with a built-in development server. It can be used to run applications without having to install a production HTTP server such as nginx or Apache. The built-in server is only designed to be used for development and testing purposes.

It can be started by using the -S flag:

# Example usage

 echo "Hello World from built-in PHP server"; 

# Configuration

To override the default document root (i.e. the current directory), use the -t flag:

php -S host/ip>:port> -t directory> 

E.g. if you have a public/ directory in your project you can serve your project from that directory using php -S localhost:8080 -t public/ .

# Logs

Every time a request is made from the development server, a log entry like the one below is written to the command line.

[Mon Aug 15 18:20:19 2016] ::1:52455 [200]: / 

# PHP CLI

PHP can also be run from command line directly using the CLI (Command Line Interface).

CLI is basically the same as PHP from web servers, except some differences in terms of standard input and output.

# Triggering

The PHP CLI allows four ways to run PHP code:

    Standard input. Run the php command without any arguments, but pipe PHP code into it:

$ php -a Interactive mode enabled php > echo "Hello world!"; Hello world!
Example.phpcode>strong> ```php  echo "Stdout 1\n"; trigger_error("Stderr 2\n"); print_r("Stdout 3\n"); fwrite(STDERR, "Stderr 4\n"); throw new RuntimeException("Stderr 5\n"); ?> Stdout 6 
$ php Example.php 2>stderr.log >stdout.log;\ > echo STDOUT; cat stdout.log; echo;\ > echo STDERR; cat stderr.log\ STDOUT Stdout 1 Stdout 3 STDERR Stderr 4 PHP Notice: Stderr 2 in /Example.php on line 3 PHP Fatal error: Uncaught RuntimeException: Stderr 5 in /Example.php:6 Stack trace: #0 thrown in /Example.php on line 6 

# Input

# Instruction Separation

Just like most other C-style languages, each statement is terminated with a semicolon. Also, a closing tag is used to terminate the last line of code of the PHP block.

If the last line of PHP code ends with a semicolon, the closing tag is optional if there is no code following that final line of code. For example, we can leave out the closing tag after echo «No error»; in the following example:

 echo "No error"; // no closing tag is needed as long as there is no code below 

However, if there is any other code following your PHP code block, the closing tag is no longer optional:

 echo "This will cause an error if you leave out the closing tag"; ?> html> body> body> html> 

We can also leave out the semicolon of the last statement in a PHP code block if that code block has a closing tag:

 echo "I hope this helps! :D"; echo "No error" ?> 

It is generally recommended to always use a semicolon and use a closing tag for every PHP code block except the last PHP code block, if no more code follows that PHP code block.

So, your code should basically look like this:

 echo "Here we use a semicolon!"; echo "Here as well!"; echo "Here as well!"; echo "Here we use a semicolon and a closing tag because more code follows"; ?> p>Some HTML code goes herep>  echo "Here we use a semicolon!"; echo "Here as well!"; echo "Here as well!"; echo "Here we use a semicolon and a closing tag because more code follows"; ?> p>Some HTML code goes herep>  echo "Here we use a semicolon!"; echo "Here as well!"; echo "Here as well!"; echo "Here we use a semicolon but leave out the closing tag"; 

# PHP Tags

There are three kinds of tags to denote PHP blocks in a file. The PHP parser is looking for the opening and (if present) closing tags to delimit the code to interpret.

# Standard Tags

These tags are the standard method to embed PHP code in a file.

# Echo Tags

These tags are available in all PHP versions, and since PHP 5.4 are always enabled. In previous versions, echo tags could only be enabled in conjunction with short tags.

# Short Tags

You can disable or enable these tags with the option short_open_tag .

    are disallowed in all major PHP coding standards

# ASP Tags

By enabling the asp_tags option, ASP-style tags can be used.

These are an historic quirk and should never be used. They were removed in PHP 7.0.

# Remarks

enter image description here

(opens new window) (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source programming language. It is especially suited for web development. The unique thing about PHP is that it serves both beginners as well as experienced developers. It has a low barrier to entry so it is easy to get started with, and at the same time, it provides advanced features offered in other programming languages.

It’s an open-source project. Feel free to get involved

Language Specification

Supported Versions

Currently, there are three supported versions

Each release branch of PHP is fully supported for two years from its initial stable release. After this two year period of active support, each branch is then supported for an additional year for critical security issues only. Releases during this period are made on an as-needed basis: there may be multiple point releases, or none, depending on the number of reports.

Unsupported Versions

Once the three years of support are completed, the branch reaches its end of life and is no longer supported.

Issue Tracker

Bugs and other issues are tracked at https://bugs.php.net/

Mailing Lists

Discussions about PHP development and usage are held on the PHP mailing lists

Official Documentation

Please help to maintain or to translate the official PHP documentation

You might use the editor at edit.php.net

Источник

How to execute PHP code using command line ?

PHP Installation for Windows Users: Follow the steps to install PHP on the Windows operating system.

  • Step 1: First, we have to download PHP from it’s official website. We have to download the .zip file from the respective section depending upon on our system architecture(x86 or x64).
  • Step 2: Extract the .zip file to your preferred location. It is recommended to choose the Boot Drive(C Drive) inside a folder named php (ie. C:\php).
  • Step 3: Now we have to add the folder (C:\php) to the Environment Variable Path so that it becomes accessible from the command line. To do so, we have to right click on My Computer or This PC icon, then Choose Properties from the context menu. Then click the Advanced system settings link, and then click Environment Variables. In the section System Variables, we have to find the PATH environment variable and then select and Edit it. If the PATH environment variable does not exist, we have to click New. In the Edit System Variable (or New System Variable) window, we have to specify the value of the PATH environment variable (C:\php or the location of our extracted php files). After that, we have to click OK and close all remaining windows by clicking OK.

PHP Installation for Linux Users:

apt-get install php5-common libapache2-mod-php5 php5-cli

PHP Installation for Mac Users:

    Mac users can install php using the following command.

curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3

After installation of PHP, we are ready to run PHP code through command line. You just follow the steps to run PHP program using command line.

  • Open terminal or command line window.
  • Goto the specified folder or directory where php files are present.
  • Then we can run php code using the following command:

We can also start server for testing the php code using the command line by the following command:

php -S localhost:port -t your_folder/

Note: While using the PHP built-in server, the name of the PHP file inside the root folder must be index.php, and all other PHP files can be hyperlinked through the main index page.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

Источник

Читайте также:  Java process thread dump
Оцените статью