PHP
Programming Language, Scripting Language
PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
Contents
- Introduction
- Installation
- Configuration
- Examples
- Environment
- Error Handling
- Environment Variables
- mail - send mail (send emails)
- Problems and Solutions
- Shell Scripts in PHP
- Testing Scripts
- Macintosh Notes
- References
- Primary source: www.php.net
- Superb companion to MySQL database
- See also Server Side Includes (SSI) for other server side technology
Installation
Extract from php.net/manual/en/install.general.php:
In case of setting up the server and PHP on your own, you have two choices for the method of connecting PHP to the server. For many servers PHP has a direct module interface (also called SAPI). These servers include Apache, Microsoft Internet Information Server, Netscape and iPlanet servers. Many other servers have support for ISAPI, the Microsoft module interface (OmniHTTPd for example). If PHP has no module support for your web server, you can always use it as a CGI or FastCGI processor. This means you set up your server to use the CGI executable of PHP to process all PHP file requests on the server.
If you are also interested to use PHP for command line scripting (e.g. write scripts autogenerating some images for you offline, or processing text files depending on some arguments you pass to them), you always need the command line executable. For more information, read the section about writing command line PHP applications. In this case, you need no server and no browser.
A blog from 2012-09-03 blog.layershift.com/which-php-mode-apache-vs-cgi-vs-fastcgi describes differences between three variants (Apache, CGI, and FastCGI) and also a fourth (PHP FPM) is mentioned in comments.
string php_sapi_name ( void ): cgi-fcgi, 3
Apache Module | CGI | FastCGI | PHP FPM | Other Server | PHPs Own Server | CLI | Unsure | |
---|---|---|---|---|---|---|---|---|
In use now (cgi-fcgi ) |
No | No | Yes | No | No | No | No | No |
PHP Interpreter | Embedded in each Apache process | External | External | |||||
PHP config in .htaccess file(s) | Yes | ? | No |
General research
- www.google.com/search?q=change+php+settings+in+htaccess
- www.google.com/search?q=php+server+api+differences
- www.sitepoint.com/how-to-override-php-configuration-settings/
- http://php.net/manual/en/configuration.changes.php
Specific modes research
- Direct module interface (SAPI or ISAPI)
- CGI
- FastCGI
- PHP FPM (FastCGI Process Manager)
- www.google.com/search?q=fpm-fcgi
- wiki.apache.org/httpd/PHP-FPM
- www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/
- stackoverflow.com/questions/4526242/what-is-the-difference-between-fastcgi-and-fpm
- cadium
- cli
- cli_server
- continuity
- https://www.google.com/search?q=php_sapi_name+continuity
- embed
- https://www.google.com/search?q=php_sapi_name+embed
- litespeed
- https://www.google.com/search?q=php_sapi_name+litespeed
- https://www.google.com/search?q=php_sapi_name+milter
Configuration
- PHP in general
- 1&1:
- How can I change my PHP settings? https://contact.1and1.com/solutions/95
- Change PHP Directives - help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/php-c37728/change-php-directives-a660257.html
For Linux Web Hosting packages, 1&1 Managed Servers, and Linux Root servers. The following article describes how to change PHP directives by editing either the php.ini or .htaccess file. - View PHP Settings and Modules - help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/php-c37728/view-php-settings-and-modules-a595886.html
- Edit the Advanced Configuration for 1&1 Managed Servers - help.1and1.com/servers-c37684/dedicated-server-managed-c37686/edit-the-advanced-configuration-for-1and1-managed-servers-a612134.html
- Disable PHP Global Variables - help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/php-c37728/disable-php-global-variables-a708570.html
- Modify the Apache Configuration - help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/htaccess-c37776/modify-the-apache-configuration-a596185.html
- google.com/search?q=1and1+php.ini+location
- google.com/search?q=1and1+php+settings+htaccess
Examples
A few examples
Hello, World! (in html-format) > Execute (sep. win/tab) |
<?php print "<html><head><title>Hello, World! Script</title></head>\n"; print "<body>\n"; print "<p>Hello, World!</p>\n"; print "</body></html>\n"; ?> |
Output from phpinfo(1). |
<?php ob_start(); // start output buffer, => nothing's displayed until buffer's closed phpinfo(1); // Show just general information (1) $php_info .= ob_get_contents(); ob_end_clean(); $php_info = str_replace("</body></html>", "", $php_info); $offset = strpos($php_info, "<table"); print substr($php_info, $offset); ?> |
Environment
Environment tests
Method | Result | Comments | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PHP Version | phpversion() | 7.4.33 | |||||||||||||
short_open_tag | ini_get('short_open_tag') | 1 | Prefer Not Set - easier and clearer handling of mixed PHP (<?php) and XML (<?xml). | ||||||||||||
|
|||||||||||||||
Timezone | get_cfg_var('date.timezone') | America/New_York |
Checks if Exists
Checks if something exists in current environment:
Example, static data, a good/true test result: | Yes | |
Example, static data, a bad/false test result: | No | |
The following rows are now live data, retrieved when page was loaded ###, and reflects the current runtime environment. | ||
PHP version: | 7.4.33 | http://php.net/manual/en/function.phpversion.php |
core module, extension_loaded('core') | Yes | (Always, unless major - major - problem...) |
gd extension'module, extension_loaded('gd') | Yes | (Always, unless major - major - problem...) |
PECL module | No | extension_loaded('pecl') https://pecl.php.net/ |
function locale_parse(): | Yes | function_exists("locale_parse") http://php.net/manual/en/locale.parselocale.php |
string locale_get_default ( void ): | Yes | function_exists("locale_get_default"), http://php.net/manual/en/locale.getdefault.php |
bool ( string $class_name [, bool $autoload = true ] ) | Yes | class_exists("locale") |
Error Handling
$my_error_reporting_enable = 1 (in script)
Code | Live result (2023-04-01T21:59:05-0400) |
---|---|
Undefined, <?php echo $some_undefined_variable;?> |
Notice: Undefined variable: some_undefined_variable in /homepages/41/d105562370/htdocs/comgt-com/lib/php/index.php on line 544 |
Missing file, <?php include "some-non-existing-file.php"; ?> |
Warning: include(some-non-existing-file.php): failed to open stream: No such file or directory in /homepages/41/d105562370/htdocs/comgt-com/lib/php/index.php on line 548 Warning: include(): Failed opening 'some-non-existing-file.php' for inclusion (include_path='.:/usr/lib/php7.4') in /homepages/41/d105562370/htdocs/comgt-com/lib/php/index.php on line 548 |
From php.net / manual / en / errorfunc.configuration.php (R:2016-02-29):
Name | Default | Changeable | Changelog | This System |
---|---|---|---|---|
error_reporting | NULL | PHP_INI_ALL | 22517 | |
display_errors | "1" | PHP_INI_ALL | (Not set) | |
display_startup_errors | "0" | PHP_INI_ALL | (Not set) | |
log_errors | "0" | PHP_INI_ALL | (Not set) | |
log_errors_max_len | "1024" | PHP_INI_ALL | Available since PHP 4.3.0. | (Not set) |
ignore_repeated_errors | "0" | PHP_INI_ALL | Available since PHP 4.3.0. | (Not set) |
ignore_repeated_source | "0" | PHP_INI_ALL | Available since PHP 4.3.0. | (Not set) |
report_memleaks | "1" | PHP_INI_ALL | Available since PHP 4.3.0. | (Not set) |
track_errors | "0" | PHP_INI_ALL | (Not set) | |
html_errors | "1" | PHP_INI_ALL | PHP_INI_SYSTEM in PHP <= 4.2.3. | (Not set) |
xmlrpc_errors | "0" | PHP_INI_SYSTEM | Available since PHP 4.1.0. | (Not set) |
xmlrpc_error_number | "0" | PHP_INI_ALL | Available since PHP 4.1.0. | (Not set) |
docref_root | "" | PHP_INI_ALL | Available since PHP 4.3.0. | (Not set) |
docref_ext | "" | PHP_INI_ALL | Available since PHP 4.3.2. | (Not set) |
error_prepend_string | NULL | PHP_INI_ALL | (Not set) | |
error_append_string | NULL | PHP_INI_ALL | (Not set) | |
error_log | NULL | PHP_INI_ALL | (Not set) | |
Additional, by COMGT: | ||||
zend_extension = "xdebug.so" | (php.ini) | (Enhanced error handling) | (Not set) |
Some research
- How do I get PHP Errors to display? stackoverflow
- Reasons why PHP would echo errors, even with error_reporting(0)? stackoverflow
- how to display php error message on web page in firefox? stackoverflow
- PHP's white screen of death, stackoverflow
- php.net- Runtime Configuration - base for table above
- php.net- error_reporting() function
- php.net- Predefined Constants
- xdebug.org
Environment Variables
how to get client's IP address in PHP - http://www.phpbuilder.com/board/showthread.php?t=10327697
reserved variables - http://ro.php.net/manual/en/reserved.variables.php
mail - send mail (send emails)
(PHP 4, PHP 5) mail — Send mail, man page: us2.php.net/manual/en/function.mail.php
mail() http://www.google.com/search?q=php+mail%28%29
( http://us2.php.net/str_replace )
Problems and Solutions
Until further notice, pls see Perl-section for more troubleshooting in general.
php-and-ssi.php - tests
Shell Scripts in PHP
If want to create scripts for use both in web server but also maybe occasionally as scripts.
!/usr/bin/env php <?php print "<html><head><title>Printenv Script</title></head>\n"; print "<body>\n"; print "Hello, World, in PHP"; print "</body></html>\n"; ?>
Important details:
1) Shebang as first line
2) (Apparently) Also must have '<?php' .. '?>' also in scripts, to work what so ever.
3) Make sure executable (e.g. chmod a+x)
Note: Difference in how works on 1&1:
With '!/usr/bin/php ' as shebang:
(uiserver):uXXX:~/comgt-com/... > ./printenv.php
X-Powered-By: PHP/4.4.9
Content-type: text/html
With '!/usr/bin/env php ' as shebang:
(uiserver):uXXX:~/comgt-com/... > ./printenv.sh.php
X-Powered-By: PHP/5.5.30
Content-type: text/html
Resources:
- google writing shell scripts in php
- blog.digitalstruct.com/ 2007/04/24/ tips and tricks php shell scripts
Testing Scripts
Click to see how the web server handles different potential issues, details that may differ on different servers. All opens in a separate window (or tab).
Script | Description |
---|---|
printhello.php | Hello, World! program |
phpinfo.php | print lots of info about runtime environment |
More extensive tests of scripts are also avaiable in the Perl-section, including one page running a number of scripts (mostly Perl).
Older Material / Archive
Macintosh Notes
(Added 2009-09-08)
Standard installation - that is part of standard Mac OS X 10.6 - still isn't including (e.g.) mcrypt. (Compare what was installed 2009-09-08 on MacMini (PHP 5.2.9, Entropy release 7) and standard OS X 10.6 (PHP 5.3.0). See all saved static phpinfo() results. Note: MacMini config retrieved via http://192.168.1.4/comgt/inuse/lib/php/phpinfo.php) Better to download and install Entropy's (which did earlier on Mac OS X 10.5). Info: http://www.entropy.ch/software/macosx/php/ Download (v5.3.0): http://www2.entropy.ch/download/Entropy%20PHP%205.3.0-3.pkg (53.4 MB, can take quite a while... At least half hour. Seems like Entropy has a slow server - other communications are working quite fine when downloading this in 2009-09-08 ~15:30 US Central Time). |
PHP is by default NOT enabled; to enable uncomment one line in httpd.conf and restart server:
Default /etc/apache2/httpd.conf (== /private/etc/apache2/httpd.conf) |
---|
... # 2008-02-27 Wed: removing preceeding '#' from line below |
References
- General: www.php.net
- webcheatsheet.com/php/ - good overview, quick nav, web-based cheat-sheet - type
- Mac-specific:
- <none yet>
- PHP Tutorial - Learn PHP: www.tizag.com/phpT
- For 10.4, older:
- 10.4: Install Apache2, PHP5, and MySQL5 http://www.macosxhints.com/article.php?story=20051214031718251