Chapter 2 PHP basic php code writing
- 2020-05-10 17:51:37
- OfStack
Standard style or short style is recommended
<?php
// The standard style
echo 'Hello World!';
?>
<?
// Brief style
echo 'Hello World!';
?>
<script language="php">
//script style
echo 'Hello World!';
</script>
2. Four ways to comment code
<?php
// Single-line comments
/*
* Multiline comment
*/
#shell Style comments
/**
* PHPdoc Style comments
*/
?>
3. Several ways to output a string to a browser
<?php
/*
* echo The output () function outputs a string to the browser
* Function return value: void
*/
echo 'echo function!';
echo('<br/>');
/*
* echo The output () function outputs a string to the browser
* Function return value: int
*/
print 'print function';
echo('<br/>');
echo print 'echo value of print function. ';
echo('<br/>');
/*
* printf The output () function outputs a string to the browser
* Function returns the length of the printed string
*/
printf("a weekend have %d days",7);
echo('<br/>');
echo printf("a weekend have %d days",7);
echo('<br/>');
/*
* sprintf The string () function saves a string to memory
* Function returns value: the saved string itself
*/
sprintf('sprintf function');
echo('<br/>');
echo sprintf('sprintf function');
echo('<br/>');
?>
Output results:
echo function test!
print function test.
print function test. 1
a weekend have 7 days
a weekend have 7 days. 23
sprintf function test
Common type indicators
type
describe
%b
Integer, shown in base 2
%c
Integer, shown as the ASCII character
%d
An integer, shown as a signed decimal number
%f
A floating point number that is displayed as a floating point number
%o
An integer, shown as a base 8 number
%s
String, which is displayed as a string
%u
An integer, shown as an unsigned decimal number
%x
An integer, shown as a lowercase base 106 number
%X
An integer, shown as an uppercase base 106 number
4. Identifiers and variables1. Basic rules for identifiers:
1) the identifier can be of any length and can be composed of any letter, number and underscore.
2) the identifier cannot start with a number.
3) in PHP, identifiers are case sensitive.
4) a variable name can be the same as a function name.
2. Variable assignment:
<?php
$sum = 0;
$total = 1.22;
$sum = $total;
echo $sum; //1.22
?>
3. Data type of variables:
Basic data type
type
The name of the
Integer
The integer
Float
Single precision floating point number
Double
And precision floating point Numbers
String
string
Boolean
Boolean
Array
An array of
Object
object
4. Type strengthPHP is a dynamic language, which is a very weak type language. It can dynamically change the type of variables while the program is running.
5. Type conversion:
Implicit type conversion:
<?php
$sum = 0;
$total = 1.22;
$sum = $total;
echo gettype ( $sum );//double
?>
Explicit type conversion:
<?php
$sum = 100;
$total = ( string ) $sum;
echo gettype ( $sum );//string
?>
Type conversion is performed using the settype() function, with the return value of 1 indicating success and null indicating failure.
<?php
$sum = 58;
echo settype ( $sum, "float" );
echo $sum; //58
echo gettype ( $sum ); //double
?>
6. Function of testing variables:
function
function
The return value
Gettype()
Gets the type of the variable
One of the basic data types
Settype()
sets the type of the variable
Bool(1:true 0:false(or ''))
Isset()
To determine whether a variable exists
Bool
Unset()
Release the given variable
Void
Empty()
Detects whether the value of 1 variable is empty
Bool
is_int() is_integer()
Detects whether the variable is an integer
Bool
Is_string()
Detects whether the variable is a string
bool
Is_numeric
Detects whether a variable is a number or a numeric string
bool
Is_null
Test whether the variable is NULL
bool
Intval()
Gets the integer value of a variable
int
Basic use of Isset()
<?php
$a = 10;
echo isset ( $a );//1
?>
<?php
echo isset ( $b );//''
?>
Basic use of Usset()
<?php
$a = 10;
unset($a);
echo isset ( $a );//''
?>
Basic use of Empty()
<?php
$a= 5;
$b =1;
$c = 0;
$d = "";
$e = array();
$f = null;
$g = "0";
$h = false;
echo empty($a);//''(false)
echo '<br/>';
echo empty($b);//''(false)
echo '<br/>';
echo empty($c);//1(true)
echo '<br/>';
echo empty($d);//1(true)
echo '<br/>';
echo empty($e);//1(true)
echo '<br/>';
echo empty($f);//1(true)
echo '<br/>';
echo empty($g);//1(true)
echo '<br/>';
echo empty($h);//1(true)
echo '<br/>';
echo empty($f);//1(true)
?>
Basic use of is_int(). Similar functions are :is_float(), is_double(), is_string(), is_bool(), is_array(), is_object(), is_resource(), is_numeric(), is_is_es264 EN (), etc.
<?php
$a = 11;
$b = 1.23;
$c = 3.1415926;
$d = "hello";
$e = false;
$f = array();
$g = null;
echo is_int($a);//1
echo '<br/>';
echo is_float($b);//1
echo '<br/>';
echo is_double($c);//1
echo '<br/>';
echo is_string($d);//1
echo '<br/>';
echo is_bool($e);//1
echo '<br/>';
echo is_array($f);//1
echo '<br/>';
echo is_null($g);//1
echo '<br/>';
echo is_numeric($a);//1
?>
Basic use of the Intval() function. Similar functions are: floatval(), strval()
<?php
$a = 22.23;
echo gettype($a);//double
echo '<br/>';
$b = intval($a);// The type does not change after conversion $a Original type
echo gettype($a);//double
echo '<br/>';
?>
<?php
$a = 22.23;
echo gettype($a);//double
echo '<br/>';
settype($a,"integer");// It changes after the type conversion $aa Original type
echo gettype($a);//integer
echo '<br/>';
?>
7. Scope of variables
Super global variable
The variable name
role
$GLOBALS
Array of all global variables
$_SERVER
Array of server environment variables
$_GET
The array of variables passed to the script via GET
$_POST
The array of variables passed to the script via POST
$_COOKIE
COOKIE variable array
$_FILES
Array of variables associated with file upload
$_ENV
Array of environment variables
$_REQUEST
The array of variables used for user input
$_SESSION
Array of session variables
8. Constant
Once 1 is defined, it cannot be changed again.
<?php
define("TOTAL",100);
echo TOTAL;//100
echo '<br/>';
define("TOTAL",200);
echo TOTAL;//100
?>
Method to view PHP predefined constants
<?php
phpinfo();
?>
Methods that reference PHP predefined constants
<?php
echo $_SERVER["SERVER_NAME"];//localhost
echo '<br/>';
echo $_SERVER["SERVER_PORT"];//8090
echo '<br/>';
echo $_SERVER["DOCUMENT_ROOT"];//D:/AppServ/www
echo '<br/>';
?>
5. Access form variables
Three common ways
<?php
echo $username;// Short style, easy to confuse with variable names, not recommended.
echo '<br/>';
echo $_POST['username'];// Medium style, 4.1.0 Version after support, recommended
echo '<br/>';
echo $HTTP_POST_VARS['username'];// Verbose style, out of date, may be eliminated in the future
?>
Posttest.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> How to get form data </title>
</head>
<body>
<form method="POST" action="demo10.php">
username:<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
</body>
</html>
6. String concatenation.
<?php
echo "the student name is : ".$_POST['username'];
echo "<br/>";
echo "welcome to "."school";
?>