PHP Learning Notes (finishing)

Source: Internet
Author: User
Tags foreach bool continue count integer numeric variables strlen

First, start learning PHP now

Old monkey to get a website, provide host space to Php+mysql majority, compare price is also relatively low, so just can learn PHP.
However, later, he said no hurry, I did not officially start. Today, by the way. Unlike Java, which is a strongly typed language, PHP is an untyped language, which is similar to _javascript.

Refer to the following sample code (adapted from PHP manual):

?

$bool = TRUE; A Boolean

$STR = "Foo"; A string

$int = 12; An integer

Echo GetType ($bool); Prints out "Boolean"

echo "\ n";

Echo GetType ($STR); Prints out "string"

echo "\ n";

$bool = 12;

Echo GetType ($bool); Prints out "integer"

/*

Here, because the value 12 is assigned to the Boolean variable bool, the type of the variable bool becomes integer, a strongly typed language like Java, and the assignment occurs only between the same type.

*/

?>

<!--[if!supportemptyparas]--> <!--[endif]-->

Second, PHP unique continue

Continue is unique in that it accepts an optional numeric parameter to decide to skip several cycles to the end of the loop.

#php_continue. php

/*

In PHP, continue is used in the loop structure to skip the remaining code in this loop and start the next loop.

This is consistent with other languages,

However, another beauty: Continue accepts an optional numeric parameter to decide to skip several cycles to the end of the loop.

*/

$i = 0;

$j = 0;

while ($i + + < 3) {//level 3

echo "Outer

\ n ";

while (1) {//level 2

echo "Middle

\ n ";

while (1) {//level 1

echo "Inner

\ n ";

Continue 3;

}

echo "This never gets output.

\ n ";

}

echo "Neither does this.

\ n ";

$j + +;

After runs continue 3,it comes to the end of level 3

}

echo "\ $j = $j";//output: $j =0

?>

Three, the array in PHP

<?php

#php_array. php

/* By default, the key of the PHP array is a non-negative integer, which is consistent with the array in most languages such as C,c++,java

* From this point of view, an array in Java is actually a default way for arrays in PHP, and PHP's array has features of the map class in Java: Key-value

xphp Manual "The array in PHP is actually an ordered graph. A diagram is a type that maps values to the keys.

*/

$array =array ("0", "1", "2", "3", "4", "5");

Print_r ($array);

/*

Output

Array

(

[0] => 0

[1] => 1

[2] => 2

[3] => 3

[4] => 4

[5] => 5

)

*/

Count the number of elements in a group with the count () function

For ($i =0, $size =count ($array); $i < $size; $i + +)

{

echo $array [$i];

echo "\ n";

}

/*

Output

0

1

2

3

4

5

*/

<!--[if!supportemptyparas]--> <!--[endif]-->

/*use foreach to loop*/

echo "foreach to Loop\n";

foreach ($array as $temp) {

Echo ($temp);

echo "\ n";

}

Output as above

<!--[if!supportemptyparas]--> <!--[endif]-->

/* Foreach Example 1:value only * *

<!--[if!supportemptyparas]--> <!--[endif]-->

$a = Array (1, 2, 3, 17);

<!--[if!supportemptyparas]--> <!--[endif]-->

foreach ($a as $v) {

Print "Current value of \ $a: $v. \ n";//This uses the escape character \, which causes $a to be output as a string

}

/*

Output

Current value of $a: 1.

Current value of $a: 2.

Current value of $a: 3.

Current value of $a: 17.

*/

<!--[if!supportemptyparas]--> <!--[endif]-->

/* foreach example 2:value (with key printed for illustration) * *

<!--[if!supportemptyparas]--> <!--[endif]-->

$a = Array (1, 2, 3, 17);

<!--[if!supportemptyparas]--> <!--[endif]-->

$i = 0; /* For illustrative purposes only * *

<!--[if!supportemptyparas]--> <!--[endif]-->

foreach ($a as $v) {

print "\ $a [$i] => $v. \ n";

$i + +;

}

$array 2=array ("A" => "Avalue", "B" => "Bvalue", "C" => "B");

Print_r ($array 2);

echo "****\n";

echo $array 2[$array 2["C"]];//

echo $array 2[$array 2[2]];//attempt to use an array subscript like Java, is invalid

echo "\n***\n";

/*output:

****

Bvalue

***

*/

$arr = Array ("foo" => "Bar", => true);

<!--[if!supportemptyparas]--> <!--[endif]-->

echo $arr ["foo"]; Bar

Echo $arr [12]; 1

?>

Variable variables, string operators, and array operators: Parts that are different from other languages

<?php

#php的可变变量

/* Variable variables are variables that can be set and used dynamically by variable names.

A variable variable obtains the value of a normal variable as the variable name of this variable variable.

Because the value of a normal variable is variable, the variable name of the variable variable is also variable.

*/

Where is the variable variable suitable for use?

$a = "Hello";//define a common variable

$ $a = "world";//define a variable variable

echo "$a \ n";//output:hello

echo "${$a}\n";//using variable variables

with echo "$hello \ n";//output:world

echo "$hello \ n";

?>

<!--[if!supportemptyparas]--> <!--[endif]-->

<?php

#php的字符串运算符

Connection operator ("." )

$a = "a";

$b = $a. " ==>second ";//now $b is" First==>second "

echo "$b \ n";

<!--[if!supportemptyparas]--> <!--[endif]-->

Connection assignment Operator (". =")

The same to $a = $a. ==>second "

$a. = "==>second";//now &a is "First==>second"

echo "$a \ n";

<!--[if!supportemptyparas]--> <!--[endif]-->

/* In fact, it can be understood that there is only one, namely the join operator

Here's the point (".") The concatenation operator and the string connector ("+") in the Java language are similar. */

?>

<!--[if!supportemptyparas]--> <!--[endif]-->

<?php

#php的数组运算符: +

/* PHP only one array operator is a + operator.

It attaches the right array to the left array, but the duplicate key values are not overwritten.

That is, the array on the left is dominant, and the part of the (right) array on which the key is duplicated is ignored

*/

$a = Array ("A" => "Apple", "B" => "banana");

$b = Array ("A" => "pear", "B" => "Strawberry", "C" => "cherry");

$a 1=array ("C" => "A1_cherry", "D" => "A1=d");

$c = $a + $b;

Var_dump ($c);

/*output:

Array (3) {

["A"]=>

String (5) "Apple"

["B"]=>

String (6) "Banana"

["C"]=>

String (6) "Cherry"

}

*/

<!--[if!supportemptyparas]--> <!--[endif]-->

$d = $a + $b + $a 1;

Var_dump ($d);

/*output:

Array (4) {

["A"]=>

String (5) "Apple"

["B"]=>

String (6) "Banana"

["C"]=>

String (6) "Cherry"

["D"]=>

String (4) "A1=d"

}

*/

?>

<!--[if!supportemptyparas]--> <!--[endif]-->

Five, NULL

Phpmanual description of NULL: "

Null

A special NULL value indicates that a variable has no value. The only possible value for a null type is NULL.

A variable is considered NULL in the following cases:

* is assigned null.

* has not been assigned a value.

* Be unset ().

A null type has only one value, which is the case sensitive keyword NULL.

"

<!--[if!supportemptyparas]--> <!--[endif]-->

Good chaos, there are also keywords in javascript: var is used to declare variables, PHP does not, the dollar sign ($) followed by a legitimate string, a PHP variable is born, as mentioned above, it has not been assigned, it should be considered: NULL. Using strlen () to try to treat it as a string and figure out its length, the PHP engine does not think it is wrong.

<?php

if (Is_null ($none))

Print "Length=". strlen ($none). " \ n ";//can output:length=0

Else

print "Undefined variable\n";//can not come here

?>

?

Phpmanual Description: (1) is_null--Detect whether the variable is null

(2) A null type has only one value, which is the case sensitive keyword NULL

<!--[if!supportemptyparas]--> <!--[endif]-->

$fo =null;

<!--[if!supportemptyparas]--> <!--[endif]-->

if (Is_null ($FO))

{//Based on above (2), not capitalized null, this should not have been done here, in fact, why?

echo ' \ $fo =null is null\n ';//output: $fo =null is null

}

$foo =null;

if (Is_null ($f)) {

echo ' \ $f =null is also null ';//out put: $f =null is also null

}

?>

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.