编写字符串检查函数,判断一个字符串是否为有效电话号码。要求:手机号码的长度为11位的数字,固定电话为开头三或四个数字后跟一个短横线后接8位数字。
<? 2 function isTel($tel) 3 { 4 5 if (strlen($tel)==11) 6 { 7 if (is_numeric($tel)) 8 { 9 echo $tel."是有效的手机号码"."<br>"; 10 } 11 else 12 { 13 echo $tel."不是手机号码"."<br>"; 14 } 15 } 16 17 18 if ($tel[3]=='-' ) 19 { 20 $a=substr($tel, 0,3); 21 $b=substr($tel, 4); 22 if (strlen($a)==3 && is_numeric($a) && strlen($b)==8 && is_numeric($b)) 23 { 24 echo $tel."是有效的电话号码"."<br>"; 25 } 26 else 27 { 28 echo $tel."不是电话号码"."<br>"; 29 } 30 } 31 32 if ($tel[4]=='-' ) 33 { 34 $a=substr($tel, 0,4); 35 $b=substr($tel, 5); 36 if (strlen($a)==4 && is_numeric($a) && strlen($b)==8 && is_numeric($b)) 37 { 38 echo $tel."是有效的电话号码"."<br>"; 39 } 40 else 41 { 42 echo $tel."不是电话号码"."<br>"; 43 } 44 } 45 } 46 47 isTel("13388888888"); 48 isTel("0575-12345678"); 49 ?>
2、设计一个 person 类,条件如下:
a) 定义protected属性:name(姓名)、age(年龄)、sex(性别);
b) 定义构造函数,实现在对象创建时输出 “I am a person.”;
c) 定义析构函数,在对象销毁时输出 “bye”;
d)定义公有方法 getInfo() ,用于输出对象的属性信息
设计一个 student 类,条件如下:
a) 继承自 person 类;
b) 定义私有属性:number(学号)、class(班级)、major(专业);
c) 定义构造函数,输出 “I am a student.” ;
d) 重载父类的 getInfo() 方法,输出本类的属性信息
<? 2 class Person 3 { 4 protected $name,$age,$sex; 5 function __construct() 6 { 7 echo "I am a person.".'<br>'; 8 $this->name="Person"; 9 $this->age=18; 10 $this->sex='男'; 11 } 12 function __destruct() 13 { 14 echo "Bye,person.".'<br>'; 15 } 16 function getInfo() 17 { 18 echo 'Name: '.$this->name.' Age: '.$this->age.' Sex: '.$this->sex."<br>"; 19 } 20 } 21 22 23 class Student extends Person 24 { 25 private $number,$class,$major; 26 function __construct() 27 { 28 echo "I am a student. ".'<br>'; 29 $this->number=1; 30 $this->class=3; 31 $this->major="软件工程"; 32 } 33 function __destruct() 34 { 35 echo "Bye,student".'<br>'; 36 } 37 function getInfo() 38 { 39 echo "Number: ".$this->number.' Class: '.$this->class.' Major: '.$this->major.'<br>'; 40 } 41 } 42 43 $a=new Person(); 44 $a->getInfo(); 45 $b=new Student(); 46 $b->getInfo(); 47 ?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
喜欢就支持一下吧