PHP/Language Basics/Php Script
Содержание
- 1 A First PHP Script
- 2 A PHP Script Including HTML
- 3 Embedding multiple PHP scripts in a single document
- 4 Hello, World!
- 5 HTML and PHP together
- 6 Instruction separation
- 7 Keywords and function names are case insensitive
- 8 Magic Constants
- 9 Mixed-Mode Processing
- 10 Multiple start and end tags
- 11 Opening and Closing Code Islands
- 12 PHP 5 Data Types
- 13 PHP supports trigonometric and logarithmic operations
- 14 Spaces, tabs, and blank lines in between statements have no effect
- 15 Spacing
A First PHP Script
<?php
phpinfo();
?>
A PHP Script Including HTML
<html>
<head>
<title>A PHP Script Including HTML</title>
</head>
<body>
<div><b>
<?php
print "hello world";
?>
</b></div>
</body>
</html>
Embedding multiple PHP scripts in a single document
<html>
<head>
<title>
<?
print "PHP-enabled page";
$variable = "Hello World!";
?>
</title></head>
<body>
<? print $variable; ?>
</body>
</html>
Hello, World!
<html>
<head><title>PHP says hello</title></head>
<body>
<b>
<?php
print "Hello, World!";
?>
</b>
</body>
</html>
HTML and PHP together
<html>
<head>
<title>HTML and PHP together</title>
</head>
<body>
<h1 align=center>
<?php
print "Welcome";
?>
</h1>
</body>
</html>
Instruction separation
<?php
echo "This is a test";
?>
<?php echo "This is a test" ?>
Keywords and function names are case insensitive
<?
// These four lines all do the same thing
print number_format(285266237);
PRINT Number_Format(285266237);
Print number_format(285266237);
pRiNt NUMBER_FORMAT(285266237);
?>
Magic Constants
Name Description
__FILE__ Name of current file
__LINE__ Current line number
__FUNCTION__ Name of current function
__CLASS__ Name of current class
__METHOD__ Name of current method
<?php
define("DEBUG", true);
function debug_print($var, $file = __FILE__, $line = __LINE__) {
if (DEBUG) {
$where = "File = $file ($line)";
switch (strtolower(substr(php_sapi_name(), 0, 3))) {
case "cli" :
echo "$where\n";
var_dump($var);
break;
default :
echo "$where<br>";
print("<pre>");
var_dump($var);
print("</pre>");
break;
}
}
}
?>
<?php
define("DEBUG", true);
function debug_print($var) {
if (DEBUG) {
switch (strtolower(substr(php_sapi_name(), 0, 3))) {
case "cli" :
var_dump($var);
break;
default :
print("<pre>");
var_dump($var);
print("</pre>");
break;
}
}
}
?>
Mixed-Mode Processing
<?php
if ($logged_in = = true) {
print "Lots of stuff here";
print "Lots of stuff here";
print "Lots of stuff here";
print "Lots of stuff here";
print "Lots of stuff here";
}
?>
<?php
if ($logged_in = = true) {
?>
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
Lots of stuff here
<?php
}
?>
Multiple start and end tags
Five plus five is:
<?php print 5 + 5; ?>
<p>
Four plus four is:
<?php
print 4 + 4;
?>
<p>
<img src="vacation.jpg" alt="My Vacation">
Opening and Closing Code Islands
<? "Hello, world!" ?>
Here is the equivalent, written using the standard open and closing tags:
<?php
print "Hello, world!";
?>
PHP 5 Data Types
Data Type Description
Boolean Stores either a true or false value
Integer Stores a numeric value that is a whole number
Double Stores a numeric value that can contain a number of decimal places (commonly called a float)
String Stores a chain of characters
Array Stores an indexed container of values
Object Stores an instance of a defined class
Resource Holds a reference to an external source
NULL Represents a variable that has no value
PHP supports trigonometric and logarithmic operations
<?php
$cos = cos(2 * M_PI); /* cos of 2*PI is 1 */
?>
Spaces, tabs, and blank lines in between statements have no effect
<?php
$name = "Paul"; print "Your name is $name\n";
$name2 = $name; $age = 20;
print "Your name is $name2, and your age is $age\n";
print "Goodbye, $name!\n";
?>
Spacing
<?php
print "test" ;
print"test";
print "test";
?>