Basic knowledge of PHP (ii)---2017-04-14

Source: Internet
Author: User
Tags echo date

1. Three types of expressions for strings:

(1) Double quotation marks

(2) Single quotation mark

(3) Angle brackets

$s = <<<a

<div style= "width:500px; height:100px; Background-color:red "></div>

A must be written shelf, one line alone

2, three kinds of error prompt way

Notice: Alert Warning: Warning Error: Wrong

3, ternary operator

(1) $a = 10;

$b = 10;

echo $a = = $b? "    OK ":" NO "; -----------OK

(2) $sex = true;

echo $sex? "        Male ":" Female "; ----------Male

4. Statements

(1) Branch statements

if () {}

if () {}else{}

if () {}else if () {}

if () {if () {}}

(2) Swich statement

Switch ($a)

{

Case 1:

echo "1111";

Break

Case 2:

echo "2222";

Break

Case 3:

echo "3333";

Break

Default

echo "4444";

}

(3) Loop statement

for ($i =0; $i <10; $i + +)

{

echo "{$i}<br>";

}

(4) While statement (for dead loops, or output specific values)

$a = 0;

while ($a <10)

{

$a + +;

echo "{$a}<br>";

}*/

5. Functions

Four elements: return type function name parameter list function body

(1) Simple function

function Test ()

{

echo "Simple function";

}

Test ();

(2) function with parameters

function Test ($a)

{

echo $a;

}

Test ("Hello"); -------------Hello.

(2) function with return value

function Test ()

{

return "Test";

}

echo Test ();------------Output Testing

(4) Function of variable parameters

function Test ()

{

$arr = Func_get_args ();//Get all parameters, return array

$sum = 0;

for ($i =0; $i <count ($arr); $i + +)

{

$sum = $sum + $arr [$i];

}

return $sum;

}

echo Test (1,2,3,4,5,6,7,8,9,10);

Note: Strongly typed language functions

Public return type (void/int/string.bool) Say (int a)

{

}

6. Generate Random Numbers

echo rand (0,10); ---------- generated by seed, the default seed is the current date

7. Date and Time

(1) Echo time (),-------------the number of seconds that have been accumulated in 1970, that is, the timestamp

(2) Echo date ("Y year M D Day H:i:s");

Y year M month D Day h 24 hour hour H 12 Hour system I minute s seconds

(3) Echo strtotime ("2017-4-14 14:52:52"); Convert a string to a timestamp

Examples:

1.

$a = 10;

$b = 3;

echo $a/$b; ------3.333333

depends on what type it is (the decimal type is the decimal type; the integer is the integral type)

2.

$a = 1;

$b = $a + +; equivalent to $b= $a; $a = $a +1 first assignment after Operation

echo $b;-----1

3.

$a = 1;

$b = + + $a;   equivalent to $a= $a +1; $b = $a; Assign value after first operation

echo $b;-----2

Basic knowledge of PHP (ii)---2017-04-14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.