Home >Backend Development >PHP Tutorial >Basic knowledge of PHP (1)_PHP tutorial

Basic knowledge of PHP (1)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:51:311050browse

These are written for junior PHP programmers or students who have just started. Veterans can pass by. Supplements and comments are welcome; reasonable opinions and criticisms are accepted.
Some of these PHP concepts are difficult to understand at first. I list them all, hoping to help some people and reduce thorns on the way forward.
1. variable variables
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 variable $a, so $$a = ${hello} = $hello = 'hello everyone';
If for $$students[1], this will cause confusion, and the PHP interpreter may not be able to understand it. Although '[' has a higher operator, the result may not be output.
A good way to write it is: ${$students[1]} = ‘Mary’;
2. array's function(array function)
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 will 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 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 (number of seconds from 1970/1/1 to now)
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 the timestamp of this time tomorrow More 'last Monday' 'lasy Year'
-------------------------------------------------- --------
checkdate($month, $day, $year); Verify whether a date is true checkdate(5,32,2012) ? 'true' : 'false'; // return false

After getting the timestamp, we need to convert it into a readable one, such as 2012/5/22
We have 2 methods date($format, $timestamp) ; strftime($format [,$timestamp])
It is recommended to use the second 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 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 $a in the test() function does not save the result of $a++, and repeated calls to test() do not increase the value of $a
The variable $a in the test1() function declares staic $a = 0, which is a static variable.
Quote: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
A static variable can only exist in the local function scope, that is, the test1() function body, but when the program leaves the test1() scope, the static variable will not lose its value, that is, the $a variable will increase by 1; When test1() is called again, $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;

Quote: In PHP global variables must be declared global inside a function if they are going to be used in that function
If these variables are to be used within a function, the global variables must be defined within that function. This can avoid a lot of trouble.

7.reference(reference)
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 this way. My mother calls me Mingming, but my boss calls me Xiaoyan. Whether it’s Mingming or Xiaoyan, it’s me.
'&' And this is how different people call our aliases, that is, references, which is equivalent to $a = {me, or the value in the memory}, $b = {leader, mother, or variable}
With & , $b points to the only and identical value of $a in memory. So no matter what your boss calls you, or what your mother calls you, you are you. Just the name is different.

So after passing the reference, we change the value of $b and also change the value of $a.
8. pass reference variable to function(pass reference parameter to function)

function ref_test(&$var){
Return $var *= 2;
}

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

When we pass parameters to a function by reference, we are not passing a copy of the variable, but the real value,
So when we call the function ref_test($a), the value of $a has been changed, so in the end $a = 20;
9. reference function return value (reference function return value)
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, and in the function body, declare a static variable $var, which can save the increased value;
$a =& increment(); This statement is the return value of the function increment() referenced by variable $a,
Like the previous reference variable, you can regard the increment() function as a variable; this becomes $a = & $b;
So both increment() and $a point to the same value, and changing either one will change the same value.


Excerpted from warcraft

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478187.htmlTechArticleThese are written for junior PHP programmers or students who have just started. Veterans can pass by. Welcome to add and Comment; accept reasonable opinions and criticisms. Some of these PHP concepts are relatively new at the beginning...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn