php 实现错字检查,PHP每日一练:编写写字符串检查函数

编写字符串检查函数,判断一个字符串是否为有效电话号码。要求:手机号码的长度为11位的数字,固定电话为开头三或四个数字后跟一个短横线后接8位数字。

2   function isTel($tel)

3   {

4

5       if (strlen($tel)==11)

6       {

7           if (is_numeric($tel))

8           {

9               echo $tel."是有效的手机号码"."
";

10           }

11           else

12           {

13           echo $tel."不是手机号码"."
";

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."是有效的电话号码"."
";

25           }

26           else

27           {

28           echo $tel."不是电话号码"."
";

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."是有效的电话号码"."
";

39           }

40           else

41           {

42           echo $tel."不是电话号码"."
";

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.".'
';

8             $this->name="Person";

9             $this->age=18;

10             $this->sex='男';

11         }

12         function __destruct()

13         {

14             echo "Bye,person.".'
';

15         }

16         function getInfo()

17         {

18             echo 'Name: '.$this->name.' Age: '.$this->age.' Sex: '.$this->sex."
";

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. ".'
';

29             $this->number=1;

30             $this->class=3;

31             $this->major="软件工程";

32         }

33         function __destruct()

34         {

35             echo "Bye,student".'
';

36         }

37         function getInfo()

38         {

39             echo "Number: ".$this->number.' Class: '.$this->class.' Major: '.$this->major.'
';

40         }

41     }

42

43     $a=new Person();

44     $a->getInfo();

45     $b=new Student();

46     $b->getInfo();

47 ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值