PHP basic syntax: ternary operator and other operators

Ternary operator and other operators

In addition, there are some special operators and symbols, which we will explain later. Maybe we need to use it later.

##$x? True code segment: Fake code Determine whether segment is true or false? True case: False case; `` (backticks) Insert command in the middle of backticks, Execute system commands, which is equivalent to the shell_exec function@Suppress errors in a single line and prevent the errors in this line from being displayed. It is not recommended for low efficiency=>Array subscript access code->Object access codeinstanceof Determine whether an object comes from a certain class, if so return true, if not return false##Ternary operator, equivalent to if...else structure. However, the ternary operator is more concisely written. The syntax format is as follows:
SymbolDescription

$x? True code segment (only one line of code can be written): Fake code segment (only one line of code can be written) ;The code is as follows:

<?php
$x = true;

$x ? $y = 5 : $y = 6;
//输出5
echo  $y;

?>

The backtick marks are even more special. We often need to display the IP address. Is it possible to display the IP address of our windows in PHP? Using backticks, you can execute our commands (but some virtual servers prohibit the execution of these command scripts):

<?php
echo '<pre>';

echo `ipconfig`;
echo '</pre>';
?>

Execute the above code to see the effect. Does it display the IP of your machine? Address and a bunch of IP-related parameters? The

@ symbol refers to single-line suppression of errors, which we will explain in future chapters. This is the understanding level.

<?php
//打开一个不存在的文件adfsafasdfasfasdfdsadf.txt,你运行一下会发现报错了。
//再前面再加上一个@符看看效果
$fp = fopen('adfsafasdfasfasdfdsadf.txt','r');

//@$fp = fopen('adfsafasdfasfasdfdsadf.txt','r');

?>

The array subscript access operator will be explained in a chapter in the future. Just understand it here:

<?php

$data = array('sina' =>'新浪' ,'sohu' => '搜狐');

?>

The object access operator -> and instanceof are both understanding levels. From now on There is also a special chapter to explain:

<?php
//实例化一个对象
$obj = new StdClass();

//判断某对象是某由某个类实例化,如果是的则执行真
if($obj instanceof stdClass){
   echo '真';
}else{
   echo '假';
}
//向obj对象中追加一个成员属性为username
$obj -> username  = 'PHP中文网';

echo $obj -> username;

?>

In all the above examples, except for the ternary operator, the @ symbol, and the backtick mark. All other learning levels are understanding and will be explained later. Just know that there is this symbol.

Continuing Learning
||
<?php //实例化一个对象 $obj = new StdClass(); //判断某对象是某由某个类实例化,如果是的则执行真 if($obj instanceof stdClass){ echo '真'; }else{ echo '假'; } //向obj对象中追加一个成员属性为username $obj -> username = 'PHP中文网'; echo $obj -> username; ?>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!