Lab1.1 - Print Statements
我就捡着我觉得有话可说的地方记录了哈。
<?php
// put your code here
echo 'Hello World!';
echo'<br>'; //换行
echo 'You can join strings '.'by using the dot'; //连接
echo '<br>';
echo 'John'.' '.'Smith';
echo '<br>';
$date = date('d/M/Y'); //日期函数
echo $date;
?>
运行结果:
- 那个换行的代码一定要用引号引住
- 点操作符是用来连接的。eg:‘John’ , ’ ','Smith’是三个字符串。要把他们连起来就需要两个点。
- PHP中变量前面要加上$ ,date()是个直接查询日期的函数,将他的输出值赋予date变量再输出。
在同一个项目中建立多个PHP文件的方法:
1.
2.再把他的属性中的扩展改成php即可
Lab1.2 - Variables & Datatypes
<?php
// put your code here
$num = 88;
$name = "Wang haohua";
$PI = 3.14;
$success = true;
echo $num." ".$name." ".$PI." ".$success." "."<br>";
echo "$num $name $PI $success <br>";
echo "My name is $name and my facourite number is $num<br>";
echo "Today's date is ". date("d m Y");
?>
运行结果:
- 由前两个echo可知:由点操作符连接起来的空格效果和再一个字符串中直接打空格是一样的。
- success是个布尔变量,所以输出的时候不显示。
- 用date函数可以规定日期的表达形式,如图:
Lab1.3 - Test Datatypes
<?php
// put your code here
$testing ;
echo "is null?".is_null($testing);
echo '<br/>';
$testing = 5;
echo "is an integer?".is_int($testing);
echo '<br/>';
$testing = 5.024;
echo "is a double?".is_double($testing);
echo '<br/>';
$testing =true;
echo "is a boolean?".is_bool($testing);
echo '<br/>';
$testing = array('apple','orange','pear');
echo "is a array?".is_array($testing);
echo '<br/>';
echo 'is numeric?'.is_numeric($testing);
echo '<br/>';
echo 'is a resource?'.is_resource($testing);
echo '<br/>';
echo 'is an array?'. is_array($testing);
echo '<br/>';
$undecided = 3.14;
echo 'is'.$undecided." a double?". is_double($undecided)."<br/>";
settype($undecided, "string");
echo 'is'.$undecided." a string?". is_string($undecided)."<br/>";
settype($undecided, "integer");
echo 'is'.$undecided." an integer?". is_integer($undecided)."<br/>";
settype($undecided, "double");
echo 'is'.$undecided." a double?". is_double($undecided)."<br/>";
settype($undecided, "bool");
echo 'is'.$undecided." a boolean?". is_bool($undecided)."<br/>";
$undecided = 3.14;
$holder = (double)$undecided;
echo 'is '.$holder." a double?". is_double($holder)."<br/>";
$holder = (string)$undecided;
echo 'is '.$holder." a string?". is_string($holder)."<br/>";
$holder = (integer)$undecided;
echo 'is '.$holder." an integer?". is_integer($holder)."<br/>";
$holder = (double)$undecided;
echo 'is '.$holder." a double?". is_double($holder)."<br/>";
$holder = (boolean)$undecided;
echo 'is '.$holder." a boolean?". is_bool($holder)."<br/>";
echo '<hr/>';
echo 'original variable type of $undecided: ';
echo gettype($undecided);
?>
运行结果:
- 这个lab想讲的是数据类型。“is_数据类型(变量)”这个函数是用来判断数据类型的,如果为true就返回1,要为false也就不再显示值了。
- gettype(变量)这个是返回数据类型
- settype(变量,“设置数据类型”) 或 “ (数据类型)变量”都是强制转换。
eg:
<?php
$a = 3.14;
settype($a, "int"); // 法一
echo $a."<br/>";
$a = 3.14;
echo (int)$a; //法二
?>
运行结果:
Lab1.4 - Operators
<?php
// put your code here
$firstname = "John";
$lastname = "Smith";
$fullname = $firstname ." " . $lastname;
echo $fullname . "<br/>";
$a = 10;
$b = 3;
$c = $a + $b;
echo $c." = ".$a." + ".$b."<br/>";
$c = $a - $b;
echo $c." = ".$a." - ".$b."<br/>";
$c = $a * $b;
echo $c." = ".$a." * ".$b."<br/>";
$c = $a / $b;
echo $c." = ".$a." / ".$b."<br/>";
$c = $a % $b;
echo $c." = ".$a." % ".$b."<br/>";
$a++;
$b--;
echo '$a is '.$a.' after increment <br/>';
echo '$b is '.$b.' after decrement <br/>';
$c = $a > $b;
echo $c."<br/>";
$c = $a < $b;
echo $c."<br/>";
$c = $a == $b;
echo $c."<br/>";
$c = $a === $b;
echo $c."<br/>";
$c = $a <= $b;
echo $c."<br/>";
$c = $a >= $b;
echo $c."<br/>";
$a = true;
$b = false;
$c = $a || $b ;
echo $c."<br/>";
$c = $a && $b ;
echo $c."<br/>";
?>
运行结果:
- PHP中echo一个值为false的布尔变量居然不显示0,醉了。
- ===是绝对等于,不但要求值相等,连值的类型也要相同,比如:
- !==是不全等于,若值相同但数据类型不同也是真,比如:
$a = ‘2’ ;
$b = 2 ;
$a != $b 是错的,因为数据类型不相同
$a !== $b 是对的,因为这个符合不完全等于
Lab1.5 - Progress Activity
<?php
// put your code here
$ID = 1824100075;
$Given_name = "haohua";
$Family_name = "Wang";
$Enrolled = true;
$code = "VDIT";
$code_name = " Diploma of Information Technology";
$unit = "VIT1204";
$point = 12.00;
$cost = 2048.00;
echo "Student ID : $ID <br/>";
echo "Given Name: $Given_name <br/>";
echo "Family Name: $Family_name <br/>";
if ($Enrolled){
echo 'Enrolled: Yes <br/>';
} else {
echo 'Enrolled: NO <br/>';
}
echo "Course Code: $code <br/>";
echo "Course Name: $code_name <br/>";
echo "Unit of Study: $unit <br/>";
echo "Credit points: ";
printf("%.2f",$point);
echo '<br/>';
echo "Total Cost: $";
printf("%.2f",$cost);
echo '<br/>';
echo 'Cost per credit point: $';
$ans = sprintf("%.2f",$cost/$point);
echo $ans;
运行结果:
- printf(“格式化”,变量)这个函数可以控制格式化输出。
- sprintf(“格式化”,变量)这个函数将格式化后的返回值传递给变量。