All about php script

Writing Your First PHP Script

In What is PHP? you took an overall look at the PHP programming language, and what you can use it for. In this tutorial, you get to play with PHP and create your very first PHP script. Along the way you’ll learn some important basic concepts of PHP.

What you’ll need

In order to start writing PHP scripts, you need access to a Web server running PHP. Your main options are:

  • Run PHP on your own computer: The easiest way to do this is to install a complete package like XAMPP. This contains the Apache Web server, along with PHP and the MySQL database engine, in one easy-to-install package. XAMPP is available for Windows, Mac OS X, and Linux. (A popular alternative on Windows is WampServer.)
  • Run your PHP scripts on your Web host: If you already have a Web hosting account that supports PHP then you can upload your PHP scripts via FTP and run them on the Web server. The advantage of this approach is that you don’t have to install anything; the disadvantage is that it’s slower to write and test your scripts.

Your first script

Here’s the PHP script that you’re going to create:

As you can see, most of this script is plain XHTML. The PHP code is inside the tags:

The tags tell the Web server to treat everything inside the tags as PHP code to run. (Everything outside these tags is sent straight to the browser as-is.)

This line of code is very simple. It uses a built-in function, echo , to display some text (“Hello, world!”) in the Web page. PHP contains hundreds of functions that you can use to write feature-rich applications.

Читайте также:  Python выход из функции досрочно

Notice the semicolon ( ; ) after the PHP code — you need to put a semicolon after each line of code in your PHP scripts.

The semicolon is optional if you’re only writing one line of code, as in this example.

Creating the script

To create your script, you’ll need to use a text editor program. Most computers come with one or more text editors built in — for example:

Copy and paste the script code listed above into a new document in your text editor, then save the file as hello.php in the document root folder — that is, the top level of your website — on your hard drive. If you’re not sure where your document root folder is then consult your Web server manual. Common locations include:

  • XAMPP on Windows: C:/Program Files/xampp/htdocs/
  • XAMPP on Linux: /opt/lampp/htdocs/
  • XAMPP on Mac OS X: /Applications/XAMPP/htdocs/

If you want to run the script on your Web hosting account rather than your own computer then you’ll need to upload the script using FTP instead.

Find out more about the document root, as well as uploading files, in Getting started with your Web hosting service.

Testing the script

Now you’re ready to run your script. To do this, open a Web browser, type the script’s URL into the browser’s address bar, and press Enter . If you’re running the Web server on your own computer then the URL will probably be:

If, on the other hand, you’re running the script on a Web hosting account then you need to use something like:

If all goes well then you should see a page similar to this:

Running your first PHP script

Problems?

If you get an error message or nothing happens, check that your Web server is set up properly (see the Web server’s documentation for help) and that you entered the script code correctly.

Читайте также:  Java sorting string set

If you see the script code displayed in the browser, rather than the expected Web page, or your browser offers to download the script file, then your Web server has not been configured to run PHP scripts.

If you’re still stuck then check out the many PHP support options available.

What next?

If you saw the correct Web page then congratulations — you’ve successfully written, saved, and run your first PHP script! You’ll build on these skills in future tutorials as you learn how to write more complex PHP scripts and applications.

You might also like to browse the PHP language reference to get some ideas for other PHP scripts to write.

Reader Interactions

Comments

Источник

PHP Syntax

A PHP script is executed on the server, and the plain HTML result is sent back to the browser.

Basic PHP Syntax

A PHP script can be placed anywhere in the document.

The default file extension for PHP files is » .php «.

A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function » echo » to output the text «Hello World!» on a web page:

Example

My first PHP page

Note: PHP statements end with a semicolon ( ; ).

PHP Case Sensitivity

In PHP, keywords (e.g. if , else , while , echo , etc.), classes, functions, and user-defined functions are not case-sensitive.

In the example below, all three echo statements below are equal and legal:

Example

Note: However; all variable names are case-sensitive!

Look at the example below; only the first statement will display the value of the $color variable! This is because $color , $COLOR , and $coLOR are treated as three different variables:

Example

$color = «red»;
echo «My car is » . $color . «
«;
echo «My house is » . $COLOR . «
«;
echo «My boat is » . $coLOR . «
«;
?>

Читайте также:  Get array len php

PHP Exercises

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

PHP Tutorial

This PHP tutorial, whether you’re a beginner or a professional, our tutorial is designed to provide you with in-depth knowledge of the PHP scripting language.

PHP Tutorial

With our PHP tutorial, you’ll learn all the important topics, including control statements, functions, arrays, strings, file handling, form handling, regular expressions, date and time manipulation, object-oriented programming in PHP, mathematical operations, working with PHP and MySQL, integrating PHP with Ajax, harnessing the power of PHP with jQuery, and more.

What is PHP?

The term PHP is an acronym for Hypertext Preprocessor . It is an open-source, interpreted, object-oriented server-side scripting language that is used for web development. PHP is developed by the Rasmus Lerdorf in 1994 with the very first version of PHP that simply designed to set the Common Gateway Interface (CGI) binaries, which are written in C programming language. The latest version of PHP is PHP versions 8 which is released on November 24, 2022. It can be easily embedded with HTML files. HTML codes can also be written in a PHP file. The PHP codes are executed on the server-side whereas HTML codes are directly executed on the browser.

Example: Simple program to print “Hello world!” message on the screen.

PHP

Источник

Оцените статью