Examples of adding, deleting, modifying and querying PHP variables and strings

Time:2020-11-10

This paper describes the PHP variable and string add, delete, modify and query operations. For your reference, the details are as follows:

Use of variables

<?php
//Tell the browser what type of document to parse with what encoding method, to prevent Chinese garbled
header("Content-type: text/html; charset=utf-8");
//Examples of integers
//Declaration integers
 
$var=123;
//Output one or more strings
echo $var;
//However, the details of dependent variables can be printed one or more variables at a time
// H:\dev\wamp64\www\demo1-test.php:9:int 123
var_dump($var);
//Declare octal integers
$var=0123;
echo $var;
var_dump($var);
 
//Declare hexadecimal integers
$var=0xff00;
echo $var;
var_dump($var);
//Multiple parameters are declared at one time and assigned the same value
$a=$b=$c=123;
//Multiple parameter printing
var_dump($a,$b,$c);
//Floating point error
$float=12.3;
var_dump($float);
//Scientific counting
$float = 2E3; // 2 times 10 to the third power 2000
var_dump($float);
The - 10th power of $float = 2e-2; // 2 is 0.02
var_dump($float);
 
//Boolean type
$bool1=true;
$bool2=TRUE;
$bool3=false;
$bool4=FALSE;
var_dump($bool1,$bool2,$bool3,$bool4);
//If statement
$dream=true;
if($dream){
 Then I'll buy a house in the Third Ring Road of Beijing;
}else {
 Echo "write code honestly";
}
//String type
//The string is written to the delimiter
//Single quotation mark, double quotation mark, heredoc nowdoc
$username='king';
$email="[email protected]";
$string = '; // empty string
var_dump($username,$email,$string); //s
 
Echo $username, '< br / > double quote parsing variable: - >';
echo "$username";
Echo "< br > single quotation does not parse variables: therefore, it is more efficient - >";
echo '$username';
// He Said "I'm Fine
//If there is a conflict between the content and the delimiter, use the escape character\
echo "<br/>";
$str="He Said \"I'm Fine";
echo "$str";
//A single quote only resolves two escape characters
$str='1\n1\t1\r1\1\'1\1111111111111111';
echo $str;
echo "<br/>";
//Double quotation marks resolve all escape characters
$str="1\n1\t1\r1\1\'1\1111111111111111";
echo $str;
 
echo "<hr/>";
$var=123;
//The value of $VaR is 123
The value of echo '$VaR is $var';
echo "<br/>";
Echo "\ $VAR has a value of $var"; ";
echo "<hr/>";
 
$username='king';
//When parsing variables, PHP will try to get as many legal characters back as possible, and think that the more you take, the clearer the meaning
//Echo "the name $username is me";
Echo "the name $username is me";
//The variables in the curly bracket PHP variable form a whole graph
Echo "the name ${username} is me";
Echo "the name {$username} is me";
 
//You can add, delete, modify and check the specified characters in the string

String operation

<?php
//Add, delete and insert the specified string in the string
$string='abcdef';
//Find
echo $string{0};
echo $string{2};
echo $string{5};
//Modification
//Note that only one-to-one modifications can be made
$string{1}='z';
echo " <br/>";
echo $string;
$string{4}='!';
echo " <br/>";
echo $string;
echo "<hr/>";
$string='abc';
$string{0}="Hello ";
echo "$string";
echo "<hr/>";
$string ='Hello ';
var_ Dump ($string); //: String 'hello' (length = 6)
echo "<hr/>";
//Delete
$string='abcef';
$string{2}='';
echo "$string";
echo "<hr/>";
$string{0}='';
echo "$string";
//Add character
echo "<br/>";
$string='abc';
$string{3}='d';
echo "$string";
 
echo "<br/>";
$string='abc';
$string{5}='e';
echo "$string";
echo "<br/>";
var_dump($string);//string 'abc e' (length=6)
/*
$string='king';
1. Output initials
2。 Change n into o in the string
3. Delete the I in the string
4. Add an exclamation mark at the end of the string
*/
echo "<br/>";
$string='king';
echo "1. Output initials:{$string{0}}";
 
$string{2}='o';
echo "<br/>";
Echo "change n in the string to o: $string";
echo "<br/>";
$string{1}='';
Echo "delete the I in the string by $string";
 
$string{4}='!';
echo "<br/>";
Echo "an exclamation mark $string will be added at the end of the string";
var_dump($string);

More interested readers about PHP related content can view the special topics of this website: summary of common functions and skills of PHP, summary of PHP string usage, Encyclopedia of PHP array operation skills, introduction course of PHP basic syntax, introduction course of PHP + MySQL database operation and summary of common database operation skills of PHP

I hope this article will help you with PHP programming.