php基础语法学习汇总

常量学习:

<?php
# function demo
function sum($x,$y){
    $z=$x+$y;
    return $z;
}

echo sum(1,2);

#define demo
echo '<br/>';
define('hello','ee');
echo hello;

# strlen function dmeo
echo '<br/>';
$str='xxxeesssss';
echo 'xxxeesssss\'s lenght is:'.strlen($str);

# _FILE_:current file name,
# _LINE_:current line number,
# _FUNCTION_:current function name,
# _CLASS_   :current class name,
# _METHOD_  :current object method name.
echo '<br/>__FILE__:';
echo __FILE__;
echo '<br/>';
echo 'dirname(__FILE__):';
echo dirname(__FILE__);
echo '<br/>__LINE__:';
echo __LINE__;
echo '<br/>__CLASS__:';
echo __CLASS__;
echo '<br/>__FUNCTION__:';
echo __FUNCTION__;
echo '<br/>__METHOD__:';
echo __METHOD__;


require(dirname(__FILE__).'\Person.php');

$persion=new Person();
//echo '<br/>';
//echo $persion::test();
echo '<br/>';
echo $persion->test();
echo '<br/>';
require(dirname(__FILE__).'\testutil.php');
echo sayHello();
echo '<br/>';
echo sayHello();

echo '<br/>';
$actors[0]='array 00';
$actors[1]='array 01';
$actors[2]='array 02';
$actors[3]='array 03';
foreach($actors as $values){
    echo '<br/>'.$values;    
    echo '<br/>$values';
    echo "<br/>$values";
    echo "<br/>".$values;
}




?>
<?php
/*
 * Created on 2015年9月21日
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class Person{
    function __construct(){
        echo '<br/>__CLASS__:';
        echo __CLASS__;
        echo '<br/>__FUNCTION__:';
        echo __FUNCTION__;
        echo '<br/>__METHOD__:';
        echo __METHOD__;
    }    
    
    public function test(){
        echo 'test';
    }
}
 
?>

 

if elseif else 

<?php
/*
 * Created on 2015年9月21日
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 $count=10;
 
 if($count<1){
     echo 'less than 1';
 }else if($count>2){
     echo 'over than 2';
 }else if($count>3){
     echo 'over than 3';
 }else if($count>9){
     echo 'over than 9';
 }else{
     echo 'less or equal 9';
 }
 
 echo '<br/>';
 
 $strVal='29298isudsfkjkwewrwer';
 if(strlen($strVal)>10){
     echo 'the lenght over 10';
 }else{
     echo 'the length no over 10';
 }
 
?>

for foreach while 

 echo 'for($i=0;$i<10;$i++){...}';
 for($i=0;$i<10;$i++){
     echo '<br/>'.$i;
 }
 
 echo '<br/>';
 echo 'foreach($a as $ b){echo $b}';
 
 $actors=array('1','2','3','4');
 foreach($actors as $val){
     echo '<br/>'.$val;
 }

 echo 'while(...){...}';
 
 $x =1;
 while($x<5){
     echo '<br/>'.$x;
     $x++;
 }
 
 echo '<br/>';
 echo 'do{...} while(...)';
 $y=10;
 
 do{
     $y--;
     echo "<br/>$y";
 }while($y>1);
 

swtich:

<?php
/*
 * Created on 2015年9月21日
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 $val=10;
 
 switch($val){
     case 1:
         echo '1';
         break;
     case 10:
         echo '10';
         break;
     default:
         echo 'other';
         break;
 }
?>

array

<?php
/*
 * Created on 2015年9月21日
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
$a =array();
$a=array("1","2",'3');
print_r("<br/>");
print_r($a);

$a =array("x","y","z",1);
print_r("<br/>");
print_r(array_keys($a,1,true));
print_r("<br/>");
print_r(array_keys($a,"f",true));
print_r("<br/>");
print_r(array_values($a));
print_r("<br/>");
var_dump(is_array($a));

$a=array('fg'=>'飞','s'=>'d');
print_r("<br/>");
var_dump(array_key_exists(0,$a));
print_r("<br/>");
var_dump(array_key_exists("s",$a));
print_r("<br/>");
var_dump(key_exists("s",$a));

print_r("<br/>");
$arr=array("f"=>"dd","ds"=>"sx","s"=>1);
if(in_array("dd",$arr)){
    echo "f exists";
}else{
    echo "not exists";
}
print_r("<br/>");
var_dump(in_array(1,$a,true));
print_r("<br/>");
var_dump(in_array("1",$a,true));

?>

mysql_conn

<?php
/*
 * Created on 2015年9月21日
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 $conn=mysql_connect('localhost','root','root');
 if(!$conn){
     die('Could not connect:'.mysql_error());
 }else{
     echo 'Connect mysql success!';
 }
 
 $isCreateDbSuc=mysql_query('create database my_db;',$conn);
 if($isCreateDbSuc){
     echo 'Create my_db success!';
 }else{
     echo 'Error creating database:'.mysql_error();
 }
 
 mysql_close($conn);
?>

 

    原文作者:spark
    原文地址: https://www.cnblogs.com/yy3b2007com/p/4833528.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞