php示例代码

来源:互联网 发布:甲基异噻唑啉酮 知乎 编辑:程序博客网 时间:2024/06/06 10:07

1、面向过程代码示例

<?phpif($animal=="dog"){ if($animal=="dog"){ return "汪";} if($animal=="cat"){return "喵";}  if($animal=="cow"){    return "哞"; }  return "暂时听不懂该动物的声音,动物学家们正在研究"; }  echo getSound("dog");//汪 echo getSound("cat");//喵 echo getSound("cow");//哞 ?>

2、面向对象的示例代码

<?php  function getSound($animal){ if($animal instanceof Animal){    return $animal->showSound(); }  return "暂时听不懂该动物的声音,动物学家们正在研究"; }  class Animal{  function showSound(){     return "不同动物应该用不同声音"; } }class Dog extends Animal{function showSound(){return "汪"; }}class Cat extends Animal{function showSound(){        return "喵";}}class Cow extends Animal{ function showSound(){        return "哞"; } }  $dog=new Dog();  echo getSound($dog);//汪 $cat=new Cat();  echo getSound($cat);//喵 $cow=new Cow();  echo getSound($cow);//哞?> 
}





                                             
1 0