Apache is a web server
Web server (or HTTP server) delivers web pages on the request to clients. This means delivery of HTML documents and any additional content that may be included by a document, such as images, style sheets and JavaScripts.
A client (web browser) initiates communication by making a request for a specific resource using HTTP and the server responds with the content of that resource or an error message if unable to do so.
HTTP (Hypertext Transfer Protocol)
HTTP functions as a request-response protocol in the client-server computing model.
HTML HyperText Markup Language
Create a file named hello.php and put it in your web server's root directory (DOCUMENT_ROOT)
hello.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Example</title></head><body>
<?php echo "Hi, I'm a PHP script!";?>
</body></html>
-----------------------------
<?php phpinfo(); ?>
-----------------------------
<?php echo $_SERVER['HTTP_USER_AGENT']; ?>
-----------------------------
<?php
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
echo 'You are using Internet Explorer.<br/>';
}?>
}?>
form.html
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p> <input type="submit" /> </p>
</form>
action.php
Hi
<?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
You are <?php echo (int)$_POST['age']; ?> years old.
--------------------------
<?php
echo 'This is a test'; // This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo 'One Final Test'; # This is a one-line shell-style comment
?>
echo 'This is a test'; // This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo 'One Final Test'; # This is a one-line shell-style comment
?>
--------------------------
<?php
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer
echo gettype($a_bool);// prints out:boolean
echo gettype($a_str);// prints out: string
// If this is an integer, increment it by four
if (is_int($an_int)) $an_int += 4;
// If $a_bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) echo "String: $a_bool";
?>
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer
echo gettype($a_bool);// prints out:boolean
echo gettype($a_str);// prints out: string
// If this is an integer, increment it by four
if (is_int($an_int)) $an_int += 4;
// If $a_bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) echo "String: $a_bool";
?>
--------------------------
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version")
echo "The version is 1.23";
// this is not necessary...
if ($show_separators == TRUE) echo "<hr>\n";
// ...because this can be used with exactly the same meaning:
if ($show_separators) echo "<hr>\n";
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version")
echo "The version is 1.23";
// this is not necessary...
if ($show_separators == TRUE) echo "<hr>\n";
// ...because this can be used with exactly the same meaning:
if ($show_separators) echo "<hr>\n";
?>
--------------------------
<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
?>
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
?>
--------------------------
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>
--------------------------
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
--------------------------
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* More complex example, with variables. */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* More complex example, with variables. */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
--------------------------
<?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
--------------------------
<?php
class foo
{
function do_foo()
{echo "Doing foo.";}
}
$bar = new foo;
$bar->do_foo();
?>
class foo
{
function do_foo()
{echo "Doing foo.";}
}
$bar = new foo;
$bar->do_foo();
?>
--------------------------
<?php
$foo = 'Bob';// Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar...
echo $bar;
echo $foo; // $foo is altered too.
?>
$foo = 'Bob';// Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar...
echo $bar;
echo $foo; // $foo is altered too.
?>
--------------------------
<?php
$a = 1;
$b = 2;
function Sum(){
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
$a = 1;
$b = 2;
function Sum(){
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
--------------------------
[] square bracket角括弧(かくかっこ) 大括弧
() parantheses 小括弧
{} curly braces 中括弧