PHP Learning Notes: Foundation

Links to the original text: https://my.oschina.net/zipu888/blog/549651
1. Basic Grammar
PHP script block to <? PHP starts with At the end, we can place the script block of PHP anywhere in the document.
Every code in PHP must end with a semicolon

2. variable
In PHP, you don't need to declare the variable before setting it, and you don't need to declare the data type of the variable to PHP. According to the way the variable is set, PHP automatically converts the variable to the correct data type.
eg:

$a = 10;
$b = 5;
$b .= $a;
echo $b;

3. operator
PHP supports all basic operators:
+    -    *    / %    ++    --
=    +=    -=    *=    /=    .=    %=
==    !=    >    <    >=    <=
&&    ||    !
eg:

$a = 10;
$b = 5;
$b .= $a;
echo $b;
4. array

PHP has three types of arrays:
Numeric arrays: arrays with numeric ID keys
Association array: Each ID key in the array associates a value
Multidimensional arrays: arrays containing one or more arrays

Digital arrays:
Each element stored in a numeric array has a numeric ID key, which can be created in two ways:
$type1 = array(1, 2, 3);

$type2[0] = 1;
$type2[1] = 2;
$type2[2] = 3;

echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2];
Association array:
Association array, where each ID key is associated with a value. There are two ways to create it:
$name_age1 = array("zhou"=>20,"huang"=>30,"yan"=>40);

$name_age2["zhou"]=20;
$name_age2["huang"]=30;
$name_age2["yan"]=40;

echo "name_age1['yan']:" . $name_age1['yan'] . " name_age2['yan']: " . $name_age2['yan'];
echo " <br>";
Multidimensional arrays:
In multidimensional arrays, each element in the main array is also an array. Each element in a subarray can also be an array, and so on.
eg:
$student = array
(
        "zhou"=>array
        (
                1,
                2,
                3
        ),
        "huang"=>array
        (
                1,
                2,
                3
        )
);
echo "student['zhou'][2]:" . $student['zhou'][2];
echo " <br>";
5. if..elseif..else ,switch
The if and switch statements of PHP are basically similar to those of version c.
$var = 2;

if ($var == 1)
  echo "if_test: 1"; 
elseif ($var == 2)
  echo "if_test: 2"; 
else
  echo "if_test: other"; 
echo " <br>";

switch ($var)
{
case 1:
  echo "switch_test: 1";
  break;
case 2:
  echo "switch_test: 2";
  break;
default:
  echo "swith_test: 3";
};
echo " <br>";
6. cycle
while: As long as the specified condition holds, the code block is executed iteratively
do...while: First execute a block of code, and then repeat the loop when the specified condition holds
for: Number of times specified by a code block is executed in a loop
foreach: Loop code blocks based on each element in the array
eg:
$name_age2["zhou"]=20;
$name_age2["huang"]=30;
$name_age2["yan"]=40;

//do...while
$i=0;
do
{
  $i++;
  echo "The number is " . $i . "<br />";
}while ($i<5);

//foreach
foreach ($name_age2 as $value)
{
  echo "Value: " . $value . "<br />";
}
7. function
The php function is similar to the c function, with parameters and return values
An example of using a function:
//function
function add($x,$y)
{
  $total = $x + $y;
  return $total;
}

echo "100 + 10 = " . add(100,10);

Complete code:
test.php
<html>
<body>

<?php
/*
* php note
*/
// php note

//variable
$txt = "hello php";
echo $txt;
echo " <br>";

//operator
$a = 10;
$b = 5;
$b .= $a;
echo $b;
echo " <br>";

//array
$type1 = array(1, 2, 3);
$type2[0] = 1;
$type2[1] = 2;
$type2[2] = 3;
echo "array1[2]:" . $type1[2] . " array2[2]: " . $type2[2];
echo " <br>"; 

//array
$name_age1 = array("zhou"=>20,"huang"=>30,"yan"=>40);
$name_age2["zhou"]=20;
$name_age2["huang"]=30;
$name_age2["yan"]=40;
echo "name_age1['yan']:" . $name_age1['yan'] . " name_age2['yan']: " . $name_age2['yan'];
echo " <br>"; 

//array
$student = array
(
        "zhou"=>array
        (
                1,
                2,
                3
        ),
        "huang"=>array
        (
                1,
                2,
                3
        )
);
echo "student['zhou'][2]:" . $student['zhou'][2];
echo " <br>";

//if
$var = 2;
if ($var == 1)
  echo "if_test: 1"; 
elseif ($var == 2)
  echo "if_test: 2"; 
else
  echo "if_test: other"; 
echo " <br>";

//switch
switch ($var)
{
case 1:
  echo "switch_test: 1";
  break;
case 2:
  echo "switch_test: 2";
  break;
default:
  echo "swith_test: 3";
};
echo " <br>";

//do...while
$i=0;
do
{
  $i++;
  echo "The number is " . $i . "<br />";
}while ($i<5);

//foreach
foreach ($name_age2 as $value)
{
  echo "Value: " . $value . "<br />";
}

//function
function add($x,$y)
{
  $total = $x + $y;
  return $total;
}

echo "100 + 10 = " . add(100,10);

?>

</body>
</html>
Test run:
Browser input: http://192.168.21.133/test.php
Output:
hello php
510
array1[2]:3 array2[2]: 3
name_age1['yan']:40 name_age2['yan']: 40
student['zhou'][2]:3
if_test: 2
switch_test: 2
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Value: 20
Value: 30
Value: 40
100 + 10 = 110

Reproduced in: https://my.oschina.net/zipu888/blog/549651

Keywords: PHP

Added by james182 on Sat, 14 Sep 2019 09:01:05 +0300