User Tools

Site Tools


php

If you want to send us your comments, please do so. Thanks
More on comments


PHP

Install PHP without Apache
PHP docs
O'Reilly PHP cookbook (First Edition, published November 2002)
Debugging PHP
Build in functions
Comparison Operators like !=
Composer, A Dependency Manager for PHP, on Wikipedia. Installation
php-fpm
W3schools tutorial
Wikibooks tutorial
Some more Tutorials and Scripts
XDebug is a PHP extension which provides debugging and profiling capabilities, Wikipedia
Read and write ods files

Run from the command line

--run, -r: Run PHP code without using script tags '<?..?>'
php -r 'echo "Hello\n";'

More examples

Run a PHP script

--file, -f: Parse and execute file
php -f script.php;

This does not work, presumed --version is a valid option:

php -f script.php --version;

Error:

Error in argument 3, char 2: no argument for option -

Run from a script

#! /usr/bin/php
 
 
// An (empty) line here results in an (empty) line in the output.
<?php
 
 
// An (empty) line here does not result in an (empty) line in the output.
echo "Hello\n";
?>

Sample PHP web page

<?php
echo "Some teskt";
echo "<br>";
echo "Some more tekst";
?>

Cannnot redeclare

PHP Fatal error: Cannot redeclare a_function() (previously declared in /home/domain/public_html/somefile.php:51) in /home/domains/public_html/somefile.php on line 60
A solution: the file somefile.php is placed in two folders of which one should not contain somefile.php

New line

This works in an offline php script

<?php
  echo "Hello\n\n\n\n`";
?>

This does not work in an online php enabled webpage

<?php
  echo "Some teskt\nSome more tekst";
  echo "Some teskt\r\nSome more tekst";
?>

We did not get the \n nor the \r\n newline to work in an online script. This works

<?php
  echo "Some teskt";
?>
<br />
 <?php
  echo "Some more tekst";
?>

You can also use an inline php echo command to get a newline like in

<?php
  echo "Some teskt";
  echo "<br>";
  echo "Some more tekst";
?>

This also works. Make sure the \n has a space before and after it like in the following example

 <?php
  echo nl2br ("Some teskt \n Some more tekst");
?>

In a Linux terminal running bash

php -r 'echo "Hello" . "\n";
php -r 'echo "Hello\n";'

Special operators

See Tokens on PHP.net

  • :: T_PAAMAYIM_NEKUDOTAYIM Scope resolution operator . Also called Paamayim Nekudotayim (Hebrew for double colon)
  • -> T_OBJECT_OPERATOR (sometimes called: Member of object)

References

Calculate

/usr/bin/php -r '$tel = 0; $tel++; echo "tel: $tel";'

Colored text

echo "\033[01;31mText in red\033[0m";

for

php -r 'for ($letters = "T"; $letters != "AE"; $letters++) { echo "$letters ";};'

printf

<code php>printf “Percentage sign °”

Debugging

Debugging PHP

  • When an error occurs it is possible that no output shows up, you get a blank screen. Not even the output of the echo commands which you inserted for debugging is shown
  • When you run the program and nothing happens start commenting out blocks of code from the end upwards until you get something that runs. Work from there downwards
  • Insert code that gives output of variables to get a better idea what is going on. Example: echo nl2br (“\n In file fof_db.php function fof_db_query 1\n”); . The 1 before the \n represents an index number
  • See PHP.net for help. Warning: The Description of a command on PHP.net can be confusing / misleading in our experience. What seems obvious is not always obvious
  • Install php-xdebug
  • Run php -l filename.php

json

Issues

Issue: “Call to undefined function json_decode()
Do: service php7.3-fpm restart

Issue: “Failed to restart php7.3-fpm.service: Unit php7.3-fpm.service not found.” Solution: T.b.d.

Errors

Timezone database is corrupt

PHP Fatal error:  date(): Timezone database is corrupt - this should *never* happen!

Solution: Check the timezone file in /usr/share/zoneinfo. The permissions should be 644. In our case the UTC file was, for some to us unknown reason, 640

Unexpected end of file

PHP Parse error:  syntax error, unexpected end of file, expecting '`' in /home/user/php/hello.php on line 11

Solution: remove the “`” (back tick) somewhere in the program

Call to undefined function

PHP Fatal error:  Uncaught Error: Call to undefined function

Solution:


Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
php.txt · Last modified: 21-12-2021 20:00 by wim