[article series] PHP daily notes – record 1


Personal records of some small problems encountered when writing PHP code will be updated from time to time;

You are welcome to point out the mistakes in time. When you put forward your opinions, you can mark the specific question number. Thank you for your encouragement;

1. Output format of float data type

$float1 = 1.123;
$float2 = 2.3e3;
$float3 = 8E-5;
var_dump($float1, $float2, $float3);    //float(1.123) float(2300) float(8.0E-5)

var_dump(number_format(8E-5, 5));    //string(7) "0.00008"

You will find that the output of 8e-5 is not 0.00008, because PHP will automatically convert some numbers with too many digits to scientific counting method

At this time, if you want to output to 0.00008 format, you can use number_ Format (), also note that this function returns a string

2. Use.The string type is automatically converted to string type

$a = 12;
$b = 1.3;
$c = true;    //Change to 1
var_dump($a . $b . $c);        //string(6) "121.31"

3. Chinese character processing of strlen() and strpos()

Strlen() returns the length of the string in bytes, one in English and three in Chinese;

English is easy to say, no special processing, Chinese can use a special MB_ strlen()

echo strlen('string');        //6
echo strlen('Hello! 'the world ');    //15 (5 Chinese characters * 3)
echo strlen('Hello! PHP');     //12 (3 Chinese characters * 3 + 3 English characters)

echo mb_strlen('Hello! 'the world ');    //5
echo mb_strlen('Hello! PHP');     //Each is counted by character

Strpos() is used to find whether a certain character appears in the specified string. If a match is found in the string, the function will return the position of the first matching character. False if no match is found.

Note that the first position of the string is 0, not 1;

Similarly, the function also has problems in Chinese and English, and needs special MB_ Strpos() function processing

echo strpos("hello php", "php");    //6
echo strpos("hello php","he");          //0

echo strpos("Hello, PHP", "p");    //9
echo mb_strpos("Hello, PHP", "p");    //3

4. Ternary operator vs null merge operator vs spaceship operator

Ternary operator

var_dump(true ? 1 : 2);        //1

//Pay attention to this way of writing: a?: B, if a is true, then the whole expression is a. here, it is easy to write as follows, resulting in wrong results
var_dump(!empty($a) ?: 2);    //int(2)

$a = 1;
var_dump(!empty($a) ?: 2);    //Bool (true) notice that the result is true

Null merge operator a?? B, a is not null, return a, otherwise it is B

$a = 0;    //0, [], ', are all original values, and null is the value set later
var_dump($a ?? 'none');    //int(0)
var_dump($c ?? 'none');  //string(4) "none"

< = > operator

var_dump(1<=>2);    //int(-1)
var_dump('b'<=>'a');    //int(1)
var_dump(5<=>5);    //int(0)
var_dump('5'<=>5);    //int(0)


Echo can output multiple dataecho 1,1.1,true,'hello';; print can only output one;

Print has a return value, which is always 1; echo has no return value;

They are all a language structure. You can add echo () / print ();

6. Here doc

$var = ['name'=>'xqw','age'=>18];
echo <<<EOF
123'abc"ef \n gh \t ijk
"hello" 'xqw' , dwdw

[article series] PHP daily notes - record 1

$var = ['name'=>'xqw','age'=>18];
$str = <<<'EOF'
123'abc"ef \n gh \t ijk
"hello" 'xqw' , dwdw

echo $str;

[article series] PHP daily notes - record 1


Integer has integer, negative integer, binary, octal, decimal, hexadecimal and so on

$int1 = 11;
$int2 = -12;
$int3 = 0x8c;    //Hexadecimal
$int4 = 047;    //Octal
var_dump($int1, $int2, $int3, $int4);    //int(11) int(-12) int(140) int(39)

PHP’s float contains double and exponential forms

$float1 = 1.123;
$float2 = 2.3e3;    //Exponential form
$float3 = 8E-5;     //Negative exponential form
$float4 = 0.00008;

var_dump($float1, $float2, $float3, $float4);    //float(1.123) float(2300) float(8.0E-5) float(8.0E-5)

var_dump(number_format(8E-5, 5));    //string(7) "0.00008"

The data printed out by object contains public, protected and private attributes, but does not contain static attributes and constants, because it belongs to a class, but only public can be accessed

class Car
    public  $color;
    private $price = 1000;
    static  $reduce = 500;
    public  $product = 'BWM';
    protected $run = 1000;
    const NAME = 'm9';

    function __construct($color='red')
        $this->color = $color;

    function what_color(){
        return $this->color;

$bwm = new Car('blue');
echo "<pre>";
/*object(Car)#1 (4) {
  string(4) "blue"
  string(3) "BWM"

// get_ object_ Vars() gets the properties and values of the object (only public) and saves them to the array
foreach (get_object_vars($bwm) as $prop => $val) {
    echo "$prop => $val",'<br>';

/*color => blue
product => BWM*/

Null is special, which simply means that the value of the variable is null

var_dump(NULL==0 ?: false);     //bool(true)
var_dump(NULL==0.0 ?: false);   //bool(true)
var_dump(NULL==""?:false);      //bool(true)
var_dump(NULL==[]?:false);      //bool(true)
var_dump(NULL==false?:false);   //bool(true)

var_dump(NULL===0 ?: false);     //bool(false)
var_dump(NULL===0.0 ?: false);   //bool(false)
var_dump(NULL===""?:false);      //bool(false)
var_dump(NULL===[]?:false);      //bool(false)
var_dump(NULL===false?:false);   //bool(false)

8,0 , 0.0 , false , []Of==compare

var_dump(0==0.0?:false);        //bool(true)
var_dump(0==""?:false);         //bool(true)
var_dump(0==false?:false);      //bool(true)
var_dump(0==[]?:false);         //Note here: bool (false)

var_dump(0.0==""?:false);       //bool(true)
var_dump(0.0==false?:false);    //bool(true)
var_dump(0.0==[]?:false);       //Note here: bool (false)

var_dump(""==false?:false);     //bool(true)
var_dump(""==[]?:false);        //bool(false)

9,'0' and false, 0, 0.0, [], ', nullOf = = comparison results

var_dump("0"==false?:false);    //bool(true)

var_dump("0"==""?:false);       //bool(false)

var_dump("0"==null?:false);     //bool(false)

var_dump("0"==0.0?:false);      //bool(true)

var_dump("0"==[]?:false);       //bool(false)

var_dump("0"==0?:false);       //bool(true)

We need to pay attention to these data0,0.0,"", '' , "" , "0" , "0.0" , false , [] , [0] , ['0'] , ['0.0'] , [false] , [""] , [0.0] , [[]] , nullUse of

10. Constants defined by define are not restricted by the namespace

Let’s look at an example


namespace demo3;
define("NAME" , "xqw");
const SEX = 28;


namespace demo4;
include_once './3.php';
use demo3;

echo NAME;    //xqw
echo demo3\SEX;    //28

This work adoptsCC agreementReprint must indicate the author and the link of this article

The wind in June