PHP Novice Summary of PHP basics

Source: Internet
Author: User
Tags arrays comments include php and php basics require
Read some of the basics of PHP, and here I summarize:

1. There are three ways to embed PHP scripts in HTML:

<scriptlanguage= "PHP" >
Embedding mode One
Echo ("Test");
</script>

?
Embedding Mode Two
echo "<br> Test2";
? >

<?php
Embedding mode Three
echo "<br> Test3";
? >
There is also an embedding method that uses the same markup <%%> as the ASP, but it is not recommended to modify the php.ini related configuration.

2, PHP comments and multi-line comments, and the same way Java annotations.

?
This is a single-line comment.
echo "Test";
/*
Here is multiline comment! Can write a lot of line comment content
*/
? >
Be careful not to have nested annotations, such as /*aaaa/*asdfa*/asdfasdfas*/, there is a problem with such a comment.

3, PHP main data types have 5 kinds, Integer,double,string,array,object.

4, function outside the call function external variables need to be declared with global, otherwise inaccessible, this is PHP and other programming language is a difference. Case code:

?
$a = 1;
FunctionTest () {
echo$a;
}
Test ();//This will not output the result "1".

Functiontest2 () {
global$a;
echo$a;
}
Test2 ();//This will output the result "1".
? >
Note: PHP can declare static variables inside a function. Uses in the same language as C.

5, variable variables, the function of the variable

?
Variable for variables
$a = "Hello";
$ $a = "world";
echo "$a $hello";//Will Output "HelloWorld"
echo "$a ${$a}";//will also output "HelloWorld"
? >

?
function of a variable

Functionfunc_1 () {
Print ("test");
}

Functionfun ($callback) {
$callback ();
}

Fun ("func_1");//This will output "test"
? >
6, PHP supports both scalar arrays and associative arrays, you can use the list () and array () to create arrays, the array subscript starting from 0. Such as:

?
$a [0]= "ABC";
$a [1]= "def";

$b ["foo"]=13;

$a []= "Hello";//$a [2]= "Hello"
$a []= "world";//$a [3]= "World"

$name []= "Jill";//$name [0]= "Jill"
$name []= "Jack";//$name [1]= "Jack"
? >
7, the correlation parameter transmission (& use), two kinds of methods. Cases:

?
Method One:
Functionfoo (& $bar) {
$bar. = "Andsomethingextra";
}
$str = "thisisastring,";
Foo ($STR);
Echo$str;//output:thisisastring,andsomethingextra

echo "<br>";
Method Two:
Functionfoo1 ($bar) {
$bar. = "Andsomethingextra";
}
$str = "thisisastring,";

Foo1 ($STR);
Echo$str;//output:thisisastring,

echo "<br>";

Foo1 (& $str);
Echo$str;//output:thisisastring,andsomethingextra
? >
8, the function default value. In PHP, the function supports setting the default value, which is the same as C + + style.

?
Functionmakecoffee ($type = "Coffee") {
echo "makingacupof$type.\n";
}
Echomakecoffee ()//"Makingacupofcoffee"
Echomakecoffee ("espresso");//"Makingacupofespresso"
/*
Note: All parameters with default values when using parameter defaults should be defined behind parameters that have no default values. Otherwise, the program will not work as expected.
*/

FunctionTest ($type = "Test", $FF) {//Error example
Return$type. $ff;
}
9, PHP's several special symbolic meaning.

$ variable
The address of the & variable (added in front of the variable)
@ Do not display error message (plus before variable)
Methods or properties of the-> class
The element value of the => array
?: Ternary operator

10,include () statement and require () statement

If you want to include files based on criteria or loops, you need to use include ().

The Require () statement is simply included once, and any conditional statement or loop is not valid for it.

Because include () is a special statement structure, if the statement is in a block of statements, you must include it in a statement block.

?
The following is an error statement
if ($condition)
Include ($file);
Else
Include ($other);

The following is the correct statement
if ($condition) {
Include ($file);
}else
{
Include ($other);
}
? >

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.