Introduction - potatoscript/php GitHub Wiki
PHP (Hypertext Preprocessor) is a widely-used, open-source server-side scripting language designed for web development. It can be embedded into HTML and is typically used to build dynamic websites and web applications. PHP is particularly powerful for server-side tasks such as form processing, database management, and user authentication.
- Server-side scripting: PHP is executed on the server, making it suitable for creating dynamic web pages.
- Cross-platform: PHP can run on various operating systems such as Windows, macOS, and Linux.
- Database support: PHP easily integrates with databases like MySQL, PostgreSQL, and SQLite.
- Embedded within HTML: PHP code can be embedded within HTML, making it easy to generate dynamic content in a web page.
- Open-source: PHP is open-source, free to use, and supported by a vast community.
- Large ecosystem: With frameworks such as Laravel, Symfony, and CodeIgniter, PHP is widely used for building modern web applications.
To get started with PHP, you need to have PHP installed on your local machine or server. Here’s how you can install PHP on different systems:
-
Download XAMPP: XAMPP is an easy-to-install package that includes Apache, MySQL, and PHP.
- Visit XAMPP's official website and download the installer for Windows.
- Follow the installation instructions to set up XAMPP on your machine.
- Start the Apache server and MySQL server using the XAMPP control panel.
-
Verify PHP Installation:
- Open your browser and navigate to
http://localhost/phpinfo.php
. - You should see a PHP information page confirming PHP is working.
- Open your browser and navigate to
-
Install PHP via Homebrew:
- Open the terminal and run the following command to install PHP:
brew install php
- Open the terminal and run the following command to install PHP:
-
Verify PHP Installation:
- Type
php -v
in the terminal to check the PHP version installed. - You can also run the PHP built-in web server:
php -S localhost:8000
- Navigate to
http://localhost:8000
in your browser.
- Type
-
Install PHP using apt-get (Debian/Ubuntu):
- Run the following command to install PHP and Apache server:
sudo apt update sudo apt install php libapache2-mod-php
- Run the following command to install PHP and Apache server:
-
Verify PHP Installation:
- Run
php -v
to check the installed PHP version. - Restart the Apache server:
sudo systemctl restart apache2
- Test PHP by creating a
phpinfo.php
file in your web server’s root directory and accessing it through a browser.
- Run
Once you’ve installed PHP, it’s time to write your first PHP script. PHP files typically have a .php
extension.
-
Open your favorite text editor (e.g., VSCode, Sublime Text).
-
Create a new file and name it
index.php
. -
Add the following code:
<?php echo "Hello, World!"; ?>
-
Save the file and place it in the appropriate directory for your server (e.g.,
htdocs
in XAMPP orwww
in other local servers). -
Open your browser and navigate to
http://localhost/index.php
.
You should see the message Hello, World!
displayed in your browser.
PHP code is embedded in HTML using the PHP tags <?php ... ?>
. The code inside these tags is executed by the server.
-
PHP tags: PHP code must be enclosed in
<?php
and?>
tags. -
Semicolons: Each statement in PHP must end with a semicolon (
;
). -
Comments: PHP supports single-line comments using
//
and multi-line comments using/* */
.
Example:
<?php
// This is a single-line comment
echo "Welcome to PHP!"; // This is another comment
/* This is a
multi-line comment */
?>
In PHP, variables are used to store data. A variable is defined by prefixing a name with a dollar sign ($
), and its value can be changed at any point.
Example:
<?php
$greeting = "Hello, PHP!";
echo $greeting;
?>
Now you have a basic understanding of PHP, how to install it, and how to write your first PHP script. As you progress, you will learn more advanced features of PHP like arrays, forms, file handling, and interacting with databases.
In the next section, we will dive deeper into PHP variables and data types, which are essential for building dynamic web applications.