php

alexmaodali發表於2024-04-04

php

<?php xxx?>

1.case-sensitivity

The following are case-insensitive in PHP:

PHP constructs such as if, if-else, if-elseif, switch, while, do-while, etc.
Keywords such as true and false.
User-defined function & class names.
On the other hand, variables are case-sensitive. e.g., $message and $MESSAGE are different variables.

2.Variables

$name="alex";
$_sex=1;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $title="this is title";
    ?>
    <h1><?php echo $title;?></h1>
    <h1><?=$title;?></h1>
</body>
</html>

3.Constant

if(condition){
    define(var,val);
}
ex: define('ORIGIN',[0,0])
or

const TAX=0.23;

Second, the define() function allows you to define a constant with the name that comes from an expression

<?php

define('PREFIX', 'OPTION');


define(PREFIX . '_1', 1);
define(PREFIX . '_2', 2);
define(PREFIX . '_3', 3);

4.Var_dump /die /function

<?php

function d($data){
    echo '<pre>';
    var_dump($data);
    echo '</pre>';
}

function dd($data){
    echo '<pre>';
    var_dump($data);
    echo '</pre>';
    die();
}
// store the variables
$title="PHP is awesome";
$name="Alexmaodali";
$age=33;

d($name);
// dd($name);
echo 'after die';
require 'index.view.php';

5.Data Types

Scalar:Boolean Integer Float String
Compund:Array Object
Special:Null Resource

PHP treats the following values as false:

The false keyword.
The integer 0 and -0 (zero).
The floats 0.0 and -0.0 (zero).
The empty string ("", '') and the string “0”.
The empty array (array() or []).
The null.
The SimpleXML objects created from attributeless empty elements.
$max=1;
$price=2.3;
$is_admin=true;
$goods=['mouse','php']//
echo $goods[0];//mouse

also:
$prices=[
    'pc'=>3000,
    'tomato'=>1,
];
$price['pc'];//3000;

is_bool(var)
is_int(var);
PHP_INT_SIZE
PHP_INT_MIN
PHP_INT_MAX

0.123E1
$total=0.1+0.1+0.1;
echo $total==0.3;//flase

String

$name="Alex";
echo 'hello'.$name;//hello Alexjj
echo 'hello $name';//hello $name;
echo "hello $name";
echo "hello {$name}";//hello Alex

echo $name[0];//A
echo strlen($name);//4

heredoc:
$final=<<<EDD
hello $name;
EDD;
newdoc:will not expand the var
$final=<<<'EDD'
hello $name;
EDD;

null

$phone=null;
var_dump($phone);//NULL
$name='Alex'
unset($name);
var_dump($name);//NULL
var_dump(is_null($name));//true
var_dump($name===null);//bool(true);

type casting

(array)
(bool)
(int)
(float)
(object)
(string)
echo (int)12.8;//12
echo (int)'he';//0
echo (int)'100 USD';//100
echo (int)null;//0
echo (string)true;//1
echo (string)false;//''
echo (string)[1,3];//Array;

type juggling

$age=33;
$name='33';
if(age==name){
    echo "age=name";//age=name
}
echo age+name;//66;
$d='33 sd';
echo age+name;//66;

6.if/for/while

<?php if ( expession) : ?>
<!-- HTML code here -->
<?php endif; ?>

if..elseif

if(xxx){

}elseif(xxx){

}
$a=10;
$b=12;
if($a>$b):
    echo "a>b";
elseif($a<$b):
    echo "a<b";
else:
    echo 'a=b';
endif;

$result = $initial ?: $default;

$result='true'?:3;//true

<?php

switch (expression) {
	case value1:
		// code block 1
		break;
	case value2:
		// code block 2
		break;
	case value3:
		// code block 3
		break;
	default:
		// default code block
}


<?php

for (start; condition; increment) {
	statement;
}

while

<?php

while (expression):
	statement;
endwhile;

<?php

while (expression)
	statement;

7.function

<?php function welcome_user($username) { ?>
	<span>Welcome <?= $username ?></span>
<?php } ?>

function name(a='alex',b){
    xxx
}
Separate parameters by a comma (,). Since PHP 8.0, the parameter list can have the trailing comma (,) which the PHP interpreter ignores.
By default, arguments are passed by value in PHP.
Prepend parameters by an ampersand (&) to pass arguments by reference.

Named parameters

function cot($text,$name){
    return $name.$text;
}
cot(
    $text:'Alex',
    "maodali"
);
PHP has four types of variable scopes including local, global, static, and function parameters.

type hint

function upper(?string str,mixed name):string|int
{
    ...
}

strict mode

declare(strict_types=1);

variable funcargs

function sum(){
    $number=func_get_args();
    ...
}

function sum(...$num){
    ...
}
function sun(int $a,int $b,int ...$num){
    ...
}

Anonymous Function

$msg='hello';
$sayhi=funcion() use ($msg){
    echo $msg;
}

Variable Functions

<?php

$f = 'strlen';
echo $f('Hello');//5

8.Array

$arr=array()
$arr=array(1,2)
$arr=[]
$arr=[1,2]
print_r($arr)

add an element:
$arr[]=1;
remove array element:
unset($arr[1])

Associate array:

$html=[]
$html['title']='php';
print_r($html['title'])

Foreach:

foreach($array_name as $element)..
foreach($array_name as $key=>$value)...

9.GET POST

$src=$_GET['name'];

10.Mysql

localhost/phpmyadmin/

相關文章