Search notes:
PHP code snippets
__FILE__
expands to the absolute path of the file where this identifier occurs.
Browsing files
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>Browse Files</title>
</head>
<body>
<?php
$q = $_GET['q'];
if (is_dir($q)) {
print "<h1>Directory $q</h1>";
$dir = dir($q);
while (false !== ($entry = $dir->read())) {
echo "<a href='?q=$q/" . $entry . "'>$entry</a><br>";
}
$dir->close();
}
elseif (is_file($q)) {
print "<h1>File $q</h1>";
print "<code><pre>";
$f = fopen($q, 'r');
while(!feof($f)) {
$txt = fread($f, 1024);
print htmlspecialchars($txt);
}
fclose($f);
print "</code></pre>";
}
else {
print "$q is neither file nor directory";
}
print "<p>";
?>
</body>
</html>
form_with_name_array.html
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>Form with name array</title>
</head>
<body>
<form action="#" method="post">
<!--
With this weird person[0][first_name] names, it's possible
to access the $_POST variable more convenently as an
array.
-->
<b>First Person</b><br>
First name: <input name="person[0][first_name]" value="Peter" />
Last name: <input name="person[0][last_name]" value="Mayer" />
<p><b>Second Person Person</b><br>
First name: <input name="person[1][first_name]" value="Diana" />
Last name: <input name="person[1][last_name]" value="Muller" />
<p><input type="submit" value="Go">
</form>
<?php
foreach ($_POST['person'] as $person_no => $person_names) {
print "<p>Person " . $person_no . ":<br>";
foreach ($person_names as $attr => $name) {
print $attr . " = " . $name . "<br>";
}
}
?>
</body>
</html>
show_errorlog.html
<html>
<head><title>pass by reference / pass by value</title></head>
<body>
<?php
$log_file = getenv('US_ROOTF') . // UniformServer Root
'/core/apache2/logs/error.log';
print "Content of <b>$log_file</b><br>";
print "<pre>";
print file_get_contents($log_file);
print "</pre>";
?>
<h1>Links</h1>
See also <a href='getenv.html'>getenv('US_ROOTF')</a>.
</body>
</html>
dir
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>dir</title>
</head>
<body>
<?php
$dir = dir("/tmp");
echo "Handle: " . $dir->handle . "<p>";
echo "Path: " . $dir->path . "<p>";
while (false !== ($entry = $dir->read())) {
echo $entry."<br>";
}
$dir->close()
?>
</body>
</html>
func_get_args
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>func_get_args</title>
</head>
<body>
<?php
func_1('foo', 'bar', 'baz');
func_2('one', 'two', 'three', 'four');
function func_1($arg_1, $arg_2) {
print "<b>func_1</b><br>\n";
foreach (func_get_args() as $arg) {
print "$arg<br>\n";
}
}
function func_2($arg_1, $arg_2) {
print "<p><b>func_2</b><br>\n";
print "arg_1 = $arg_1<br>\n";
print "arg_2 = $arg_2<br>\n";
$additional_args = array_slice(func_get_args(), 2);
foreach ($additional_args as $additional_arg) {
print "additional arg=$additional_arg<br>\n";
}
}
?>
</body>
</html>
loop_with_inner_html.html
<html><head><title>loop with inner html</title>
<style type='text/css'>
div {
border: 2px #a4c solid;
margin-bottom: 5px;
}
</style>
</head>
<body>
<?php for ($i = 0; $i < 10; $i++) { ?>
<div> one of ten divs </div>
<?php } ?>
</body>
</html>
define
<html>
<head><title>pass by reference / pass by value</title></head>
<body>
<?php
if (defined('FOO')) {
print "FOO is defined<br>";
}
else {
print "FOO is not defined<br>";
}
define ('FOO', 'bar');
print "FOO = " . FOO;
?>
</body>
</html>
dollar_dollar.html
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>$$</title>
</head>
<body>
Demonstrating <b>variable variables</b>:<p>
<?php
$foo = 'original foo';
$bar = 'original bar';
$baz = 'original baz';
$var_name = 'bar';
$$var_name = "<b>changed</b> $var_name";
print "foo: $foo<br>";
print "bar: $bar<br>";
print "baz: $baz";
?>
</body>
</html>
show_source
<html>
<head><title>show_source</title></head>
<body>
<?php
show_source($_SERVER['SCRIPT_FILENAME']);
?>
</body>
</html>
gettype
<html><head><title>gettype</title>
<style type="text/css">
td {margin-left: 0.5em;}
</style>
</head>
<body>
<?php
class CLS { }
$obj = new CLS;
?>
<table summary='gettype'>
<tr><td>42 </td><td><?php echo gettype( 42 ); ?></td></tr> <!-- integer -->
<tr><td>42.42 </td><td><?php echo gettype( 42.42 ); ?></td></tr> <!-- double -->
<tr><td>'hello world' </td><td><?php echo gettype('hello world' ); ?></td></tr> <!-- string -->
<tr><td>TRUE </td><td><?php echo gettype( TRUE ); ?></td></tr> <!-- boolean -->
<tr><td>array('one' ) </td><td><?php echo gettype( array('one' ) ); ?></td></tr> <!-- array -->
<tr><td>array('one'=> 1) </td><td><?php echo gettype( array('one'=> 1) ); ?></td></tr> <!-- array -->
<tr><td>NULL </td><td><?php echo gettype( NULL ); ?></td></tr> <!-- NULL -->
<tr><td>$obj </td><td><?php echo gettype( $obj ); ?></td></tr> <!-- object -->
<tr><td>$undefined_var </td><td><?php echo gettype( $undefined_var ); ?></td></tr> <!-- NULL -->
<tr><td>gettype(0) </td><td><?php echo gettype( gettype(0) ); ?></td></tr> <!-- string -->
</table>
</body>
</html>
file_upload.html
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>File upload</title>
</head>
<body>
<form action="file_uploaded.html" method="post" enctype="multipart/form-data">
<table>
<tr><td>File:</td><td><input name="chosenFile" type="file" size="50" accept="text/*"></td>
<tr><td></td><td><button type="submit">Go!</button></td></tr>
</table>
</form>
</body>
</html>
file_uploaded.html
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>Post answer</title>
</head>
<body>
The content of <b><?php echo $_FILES['chosenFile']['name']; ?></b> is: <pre><code><?php echo file_get_contents($_FILES['chosenFile']['tmp_name']); ?></code></pre>
<p><a href='file_upload.html'>Upload another file</a>.
</body>
</html>
operator_precedence.html
<html><head><title>operator precedence</title></head>
<body>
<?php
function b($t) {
print $t ? "<span style='background-color:#7f7'>true</span>" : "<span style='background-color:red'>false</span>";
}
function test_1 ($b1, $b2, $b3) { ?>
<table border = 1>
<tr><td><pre> <?php b($b1); ?> && <?php b($b2); ?> || <?php b($b3); ?> </pre></td><td><pre><?php b ( $b1 && $b2 || $b3 ); ?></pre></td></tr>
<tr><td><pre>(<?php b($b1); ?> && <?php b($b2); ?>) || <?php b($b3); ?> </pre></td><td><pre><?php b ( ($b1 && $b2) || $b3 ); ?></pre></td></tr>
<tr><td><pre> <?php b($b1); ?> && (<?php b($b2); ?> || <?php b($b3); ?>)</pre></td><td><pre><?php b ( $b1 && ($b2 || $b3) ); ?></pre></td></tr>
</table>
<p>
<?php }
?>
<h1>Operator precedence</h1>
&& has higher precedence as ||<p>
<?php
for ($b1=0;$b1<2;$b1++) {
for ($b2=0;$b2<2;$b2++) {
for ($b3=0;$b3<2;$b3++) {
test_1($b1 == 0, $b2 == 0, $b3 == 0);
}}}
?>
</body>
</html>
<html><head><title>strip_tags</title></head>
<body>
<?php
$html_snippet = "Now, for <i>something</i> very <b>bold</b>!";
# Don't remove <b> tags, but remove the other tags.
# $html_rest becomes "Now, for something very <b>bold</b>!":
$html_rest = strip_tags($html_snippet, "<b>");
echo '$html_rest: ' . htmlspecialchars($html_rest);
?>
<p>See also <a href='htmlspecialchars.html'>htmlspecialchars.html</a>.
</body>
</html>
handler.html
<html>
<head><title>Handler</title></head>
<body>
<?php
print ('The requsted URI is: <b><code>' . $_SERVER['REQUEST_URI'] . '</code></b> (<code>$_SERVER["REQUEST_URI"])');
?>
</body>
</html>
phpinfo
<?php
// phpinfo prints a complete html page (with <html> etc tag)
// and a <DOCTYPE ... declaration.
phpinfo();
?>
phpversion
<html>
<head><title>phpversion()</title></head>
<body>
<?php
print "phpversion is: " . phpversion();
?>
</body>
</html>
form_handling.html
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>Form Handling in PHP</title>
</head>
<body>
<form action="#" method="post">
<table>
<tr><td>This input does not use <a href='htmlspecialchars.html'>htmlspecialchars</a>:</td><td> <input type="text" name="text_1" value="<?php echo isset($_POST['text_1']) ? $_POST['text_1'] : '<foo>&amp;<bar>'; ?>" ></td></tr>
<tr><td>This input does: </td><td> <input type="text" name="text_2" value="<?php echo isset($_POST['text_2']) ? htmlspecialchars($_POST['text_2']) : '<foo>&amp;<bar>'; ?>" ></td></tr>
</table>
<br><input type="submit" value="go">
</form>
</body>
</html>
class
<html>
<head><title>a php class</title></head>
<body>
<?php
class aClass {
var $foo;
var $bar;
var $baz;
function show() {
print "Foo: " . $this->foo . "<br>";
print "Bar: " . $this->bar . "<br>";
print "Baz: " . $this->baz . "<p>";
}
function init($foo_, $bar_, $baz_) {
$this->foo = $foo_;
$this->bar = $bar_;
$this->baz = $baz_;
}
}
$obj1=new aClass;
$obj2=new aClass;
$obj1->init("one" , "two" , "three");
$obj2->init("eins", "zwei", "drei" );
$obj1->show();
$obj2->show();
?>
<p>See also <a href='class__constructor.html'>class__constructor.html</a>.
</body>
</html>
class constructor
<html>
<head><title>pass by reference / pass by value</title></head>
<body>
<?php
class class_with_constructor {
public function __construct($foo_, $bar_, $baz_) {
$this->foo = $foo_;
$this->bar = $bar_;
$this->baz = $baz_;
}
public function show($title) {
print "<h1>$title</h1>";
print "Foo: " . $this->foo . "<br>";
print "Bar: " . $this->bar . "<br>";
print "Baz: " . $this->baz . "<br>";
}
}
$obj_1 = new class_with_constructor('foo', 'bar', 'baz' );
$obj_2 = new class_with_constructor('one', 'two', 'three');
$obj_1->show('obj_1');
$obj_2->show('obj_2');
?>
<p>See also <a href='class.html'>class.html</w>.
</body>
</html>
getcwd
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>getcwd</title>
</head>
<body>
<?php
print 'getcwd(): ' . getcwd();
?>
</body>
</html>
stripslashes
<html>
<head><title>stripslashes()</title></head>
<body>
<?php
$str = 'foo\'bar\\baz/qux';
echo "$str<br>\n";
$str_ = stripslashes($str);
echo $str_;
?>
</body>
</html>
Pass by reference / by value
<!--
http://www.adp-gmbh.ch/php/pass_by_reference.html
-->
<html>
<head><title>pass by reference / pass by value</title></head>
<body>
<?php
function pass_by_value ( $param) { array_push($param, 'val1', 'val2'); }
function pass_by_reference(&$param) { array_push($param, 'ref1', 'ref2'); }
$ar = array('foo', 'bar', 'baz');
pass_by_value ($ar);
pass_by_reference($ar);
foreach ($ar as $elem) {
print "<br>$elem";
}
?>
</body>
</html>
global
<html><head><title>global Variables</title></head>
<body>
<?php
global $one;
global $thr;
$one = "one outside of function";
$two = "two outside of function";
$thr = "thr outside of function";
f();
print "<h1>Returned from f()</h1>";
print "one: $one<br>";
print "two: $two<br>";
print "thr: $thr<br>";
function f() {
print "<h1>in f()</h1>";
global $thr;
print "one: $one<br>";
print "two: $two<br>";
print "thr: $thr<br>";
$one = "one inside of function";
$two = "two inside of function";
$thr = "thr inside of function";
}
?>
</body>
</html>