User Tools

Site Tools


programming:php

This is an old revision of the document!


PHP Programming

PHP is a web-native language that essentially embeds itself within HTML.

While the wiki is still being built, refer to: https://www.devdungeon.com/topics/php and https://github.com/DevDungeon/Cookbook/tree/master/php

Installing

In Debian and most Linux distributions, it's as simple as using the package manager:

apt install php php-pdo-sqlite
php --version
 
# For Apache2 httpd
apt install apache2 libapache2-mod-php

See the web servers page for more info on configuring

Built-in webserver

PHP has a built-in web server that is useful for local developing and testing.

# Serves current directory, no directory listing
php -S localhost:9999
 
# Specify path to serve
php -S localhost:9999 -t /path/to/serve

Phar files

Phar files are essentially tarballs of PHP content. In addition to the convenience of having a single .phar containing many files, the phar can be marked executable and treated like an executable Java JAR. See the official phar documentation.

It can be treated as a special protocol for referencing files like this: phar:myphar.phar/file.php. You can read or include files in this manner. ===== Composer ===== Refer to: https://www.devdungeon.com/content/php-composer-basics ===== Code examples ===== Here are some snippets for reference. ==== Hello world ==== Here is a simple “Hello world” application. <code php hello.php> <?php echo “Hello, world!”; </code> ==== Show all errors ==== <code php show_errors.php> <?php Add this to turn on all errors (disable in production!) The ini_set options could also go in php.ini ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); </code> ==== Functions ==== <code php functions.php> <?php function greet($name, $greeting=“Hi”) { print(“$greeting, $name!”); } greet(“NanoDano”); Hi, NanoDano! </code> ==== Include a PHP file ==== <code php load_lib.php> <?php Pick one method for loading another PHP file include('my_lib.php'); Include if available require('my_lib.php'); Fail if not available require_once('my_lib.php'); Ensure not loaded twice </code> ==== Forms ==== <code php form.php> <html> <body> <?php if ($_POST['something']) { print(“Received: ” . $_POST['something']; } <form method=“post”> <input type=“text” name=“something” id=“something” /> <br /> <button type=“submit”>Submit</button> </form> </body> </html> </code> ==== Redirect ==== Use the PHP function header() to send a new Location header. <code php redirect.php> <?php Set HTTP header to redirect Must go before any content is output header('Location: /'); Defaults to 302 Or specify status code header('Location: /', TRUE, 301); exit; If you want to ensure nothing else gets output </code> ==== SQLite database ==== This example is for SQLite3 using the PHP PDO_SQLITE, but essentially the same for other databases too. apt install php-pdo-sqlite <code php sqlite_example.php> <?php Connect (Use `sqlite::memory:` for memory-only) $db = new PDO('sqlite:'.DIR.'/bookmarks.sqlite3'); Create table $db→exec( “CREATE TABLE IF NOT EXISTS urls ( url TEXT, notes TEXT )” ); Prepared statement for changes $query = $db→prepare('INSERT INTO urls (url) VALUES (?)'); $query→execute(['https://www.devdungeon.com/']); Query $result = $db→query('SELECT * FROM urls'); foreach ($result as $result) { print($result['url'].'<br />'); } $db = null; Close it </code>

programming/php.1618364076.txt.gz · Last modified: 2021/04/14 01:34 by nanodano