Basic knowledge of PHP (1) _php tutorial

Source: Internet
Author: User
Tags ming time and date
These are addressed to junior PHP programmers or beginners, who can float past, welcome additions and comments, and accept reasonable opinions and criticisms.
Some of these PHP concepts are just beginning to be difficult to understand, I have listed them, hoping to help some people, on the way forward less thorns.
1. Variable variables (variable variable)
variable_variables.php

View Code

$a = ' Hello ';
$hello = ' Hello everyone ';

echo $ $a. '
';

$b = ' John ';
$c = ' Mary ';
$e = ' Joe ';

$students = Array (' B ', ' C ', ' e ');

echo ${$students [1]};
/*
foreach ($students as $seat) {
echo $ $seat. '
';
}


$ $var [1]
${$var [1]} for #1

*/


$a = ' Hello ';
Assign hello to the variable $a, so $ $a = ${hello} = $hello = ' Hello everyone ';
If this is confusing for $ $students [1], PHP's interpreter may not understand, ' [' Although there is a higher operator, the result may not be output.
The good wording is: ${$students [1]} = ' Mary ';
2. Array ' s function (arrays functions)
array_functions.php
View Code

Echo '

Shift & Unshift

';
$numbers = Array (1,2,3,4,5,6);
Print_r ($numbers);
Echo '
';

Shifts first elemnt out of an array
The index would reset
$a = Array_shift ($numbers);

Echo ' A: '. $a. '
';
Print_r ($numbers);

Push element to the front of array
Returns the count of array and reset array index
$b = Array_unshift ($numbers, ' first ');
Echo '
B: '. $b. '
';
Print_r ($numbers);

Echo '

';
Echo '

Pop & Push

';
Pop the last element out of array
$c = Array_pop ($numbers);
Print_r ($numbers);
Echo '
';

Push the element to the last of the array
$d = Array_push ($numbers, ' last ');
Echo ' d: '. $d. '
';

Print_r ($numbers);

3. Dates and times (time and date)
There are 3 ways to create a Unix time (the number of seconds from 1970/1/1 to the present)
Time (); Returns the current timestamp
Mktime ($hr, $min, $sec, $month, $day, $year); Mktime (6,30,0,5,22,2012) returns the timestamp of 2012 5/22 6:30:00
Strtotime ($string); Strtotime ("+1 Day") returns tomorrow at this time of the timestamp more ' last Monday ' Lasy year '
----------------------------------------------------------
Checkdate ($month, $day, $year); Verify that a date is true checkdate (5,32,2012)? ' True ': ' false '; return False

After getting the timestamp, we need to translate it into readable, such as 2012/5/22
We have 2 ways to date ($format, $timestamp); Strftime ($format [, $timestamp])
Recommended to use the 2nd type, strftime ("%y-%m-%d%h:%m:%s"); return 2012-05-22 15:46:40


5. Server variables (server and execution Environment information)
$_server
server_variables.php
View Code

Echo ' SERVER details:
';
echo ' server_name: '. $_server[' server_name ']. '
';
Echo ' Server_add: '. $_server[' server_addr ']. '
';
Echo ' Server_port: '. $_server[' Server_port ']. '
';
Echo ' Document_root: '. $_server[' Document_root ']. '
';
Echo '
';

Echo ' Page details:
';
Echo ' REMOTE_ADDR: '. $_server[' remote_addr ']. '
';
Echo ' Remort_port: '. $_server[' Remote_port ']. '
';
Echo ' Request_uri: '. $_server[' Request_uri ']. '
';
Echo ' query_string: '. $_server[' query_string ']. '
';
Echo ' Request_method: '. $_server[' Request_method ']. '
';
Echo ' Request_time: '. $_server[' Request_time ']. '
';
Echo ' http_user_agent: '. $_server[' http_user_agent ']. '
';
Echo '
';

6.variable_scope (Scope of the variable global static)
static_variables.php
View Code

function test ()
{
$a = 0;
echo $a;
$a + +;
}

Test ();
Echo '
';
Test ();
Echo '
';
Test ();
Echo '
';

Echo '

';
function Test1 ()
{
static $a = 0;
echo $a;
$a + +;
}

Test1 ();
Echo '
';
Test1 ();
Echo '
';
Test1 ();
Echo '
';

The variable in the test () function $a did not save the result of $a + + and repeated calls to test () did not increase the value of the $a
The variable $a in the Test1 () function declares Staic $a = 0 as a static variable.
Reference: A static variable exists only with a local function scope, but it does not lose its value when program execution leaves t His scope.
A static variable can only exist within the local function scope, which is the test1 () function body, but when the program leaves the Test1 () scope, the static variable does not lose its value, that is, the $a variable will increase by 1, and when the Test1 () is recalled, $a = 1;
global_variables.php
View Code

$a = 1;
$b = 2;

function Sum ()
{
Global $a, $b;

$b = $a + $b;
}

Sum ();
Echo $b;
Echo '

';
$a = 1;
$b = 2;

function Sum1 ()
{
$GLOBALS [' b '] = $GLOBALS [' a '] + $GLOBALS [' B '];
}

Sum1 ();
Echo $b;

Reference: In PHP Global variables must is declared global inside a function if they is going to being used in this function
If these variables are to be used in a function, the global variable must be defined in the function that is used. This can avoid a lot of trouble.

7.reference (citation)
variable_reference.php
View Code

$a = ' arist ';
$b = $a;
$b = ' Ming ';

echo "My name is:{$a}. But my mother call me {$b}.
";

Echo '

';


$a = ' arist ';
$b = & $a;
$b = ' Ming ';

echo "My name is:{$a}. And my mother call me {$b}.
";

This concept can be understood, my mother calls me clearly, but my leader will call me small words, whether it is Ming or small words, it is me.
' & ' And this is the way that different people call our aliases, which is a reference, equivalent to $a = {I, or the memory value}, $b = {leader, mom, or variable}
By &, $b points to $ A and the only value that is the same in memory. So no matter what your leader calls you, or what your mother calls you, you are you. It's just a different name.

So by referencing, we change the value of $b and also change the value of $ A.
8. Pass reference variable to function (passing reference parameters to functions)

Function Ref_test (& $var) {
return $var *= 2;
}

$a = 10;
Ref_test ($a);
echo $a;

When we pass arguments to a function by reference, we pass the ground not a copy of the variable, but the actual value,
So when we call the function ref_test ($a) has changed the value of the $a, so the last $a = 20;
9. Reference function return value (reference function return values)
reference_function_return_value.php

function &increment () {
static $var = 0;
$var + +;
return $var;
}

$a =& increment (); 1
Increment (); 2
$a + +; 3
Increment (); 4
echo "A: {$a}";

First, declare a reference function, in the function body, declare a static variable $var, you can save the added value;
$a =& increment (); This statement is the return value of the variable $ A reference function increment ().
As with the previous reference variable, you can think of the increment () function as a variable, so that it becomes $a = & $b;
So increment () and $a all point to the same value, and any change can change the same value.


Excerpted from Warcraft

http://www.bkjia.com/PHPjc/478187.html www.bkjia.com true http://www.bkjia.com/PHPjc/478187.html techarticle these are addressed to junior PHP programmers or beginners, who can float past, welcome additions and comments, and accept reasonable opinions and criticisms. Some of these PHP concepts are just beginning to compare ...

  • Related Article

    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.