PHP课程笔记-基础知识全

课程

本课程为PHP零基础入门,课程来自freeCodeCamp.org,课程链接:PHP Programming Language Tutorial - Full Course

笔记

Writing HTML

PHP can be inserted into body tag of HTML:

<?php
echo "Hello World"; //print out sth
echo "<p>This is my site</p>" //execute in order
?>
Variables
$characterName = "John"; // $ is used to assign variable
$characterAge = 35;
echo "A man called $characterName <br>";
echo "He is $characterAge years old";
$characterName = "Mike";
echo "Another man called $characterName <br>";
Data Types: string, integer, float, boolean, null

PHP string functions: More

echo strtolower($phrase); // lowercase
echo strlen($phrase); // length
echo $phrase[0]; // first letter
$phrase[0] = "B" // modify the first letter to B
echo str_replace("Giraffe", "panda", $phrase);
echo substr($phrase, 8, 3);

math functions:

echo abs(-200);
echo pow(2, 4);
echo sqrt(256);
echo max(2, 20);
echo round(3.7);
echo ceil(3.3);
echo floor(3.4);
Getting input from user
// setup the form
<form action="site.php" method="get">
	Name: <input type="text" name="username">
	<br>
	Age: <input type="number" name="age">
	<input type="sumbit">
</form>
Your name is <?php echo $_GET["username"] ?>
Your age is <?php echo $_GET["age"] ?>
Building a simple calculator
// setup the form
<form action="site.php" method="get">
	<input type="number" name="num1">
	<br>
	<input type="number" name="num2">
	<input type="sumbit">
</form>
Answer: <?php echo $_GET["num1"] + $_GET["num2"]?>

Actually whenever we submit a form, it will appear in the URL.

URL Parameters

store information in the URL:

// /site.php?username=Mike&age=30
// /site.php?username=Dave
echo $_GET["age"];
POST vs GET: show in url or not
<form action="site.php" method="post">
	<input type="password" name="password">
// post: not gonna show up in the url
// pass in information more safely
</form>
Arrays
$friends = array("Kevin",  2, false, "Karen", "Oscar", "Jim");
echo $firends; // ouput: Array
echo $firends[3]; // "Karen"
$friends[2] = 400;
echo $firends[2]; // 400
$friends[6] = "Mary"; // add an element
echo count($friends);
Using Checkboxes
<form action="site.php" method="post">
	Apples: <input type="checkbox" name="fruits[]" value="apples"><br>
	Oranges: <input type="checkbox" name="fruits[]" value="oranges"><br>
	Pears: <input type="checkbox" name="fruits[]" value="pears"><br>
	<input type="submit">
</form>
// Store only the checked values in an ordered array:
<?php
	$fruits = $_post["fruits"];
	echo $fruits[0]; //print out the first fruit that is checked;
?>
Associative Arrays
$grades = array("Jim"=>"A+", "Pam"=>"B-"); //store key, value pairs
$grades["Jim"] = "F"; // change value
echo $grades["Jim"]; // keys should be unique
echo count($grades);

Use with HTML: get user input and access information in an associative array.

<form action="site.php" method="post">
	<input type="text" name="student"><br>
	<input type="submit">
</form>
<?php
	$grades = array("Jim"=>"A+", "Pam"=>"B-");
	echo $grades[$_post["student"]]; 
?>
Functions
function sayHi($name, $age){
	echo "Hello $name, you are $age <br>";
}
sayHi("Mike", 40); // calling the function
sayHi("Oscar", 30); // reuse the code
Return Statements
function cube($num){
	return $num * $num * $num; // break out the function too
	echo "Hello";
}
echo cube(4);
If Statements
$isMale = true;
$isTall = true;
if ($isMale && $isTall){
	echo "You are a tall male";
} elseif ($isMale && !$isTall) {
	echo "your are a male but not tall";
} else {
	echo "your are not a tall male";
}
Switch Statements
<?php
switch ($i) {
    case "apple":
        echo "i is apple";
        break; // break us out of the structure
    case "bar":
        echo "i is bar";
        break;
    case "cake":
        echo "i is cake";
        break;
    default:
    	echo "Invalid"; // if none of these cases
}
?>
While Loops
<?php
$x = 1;
while($x <= 5) {
    echo "The number is: $x <br>";
    $x++;
}
?>

Do while loops: do sth first and then check the while condition

$x = 1;
do {
    echo "The number is: $x <br>";
    $x++;
} while ($x <= 5);
For Loops

Specifically designed to loop through index.

<?php
for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";
}
?>

Loops through an array: More

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value <br>";
}
?>
Including HTML

To include a file inside our php file:

// Reuse the same header and footer
<?php include "header.html"?>
<p>Hello</p>
<?php include "footer.html"?>
Include: PHP
<h2><?php echo $title; ?></h2>
<h4><?php echo $author; ?></h4>

Use functionality and variables in another PHP file:

// Reuse the same header and footer
<?php 
	$title = "My First Post";
	$title = "Mike";
	include "article-header.php"; // similar to "import" in python
?>
Classes & Objects

We can create or own custom datatype. A string can only represent a string, a number is only a number. But we can create a “phone”, a “book”…

// create a data type "Book"
<?php 
	class Book {
		var $title;
		var $author;
		var $pages;
	}
	$book1 = new Book; // creating a "Book" called book1, which is an object
	$book1->title = "Harry Potter";
	$book1->author = "JK Rowling";
	$book1->pages = 400; // it's just like we create a string
?>
Constructors
<?php 
	class Book {
		var $title;
		var $author;
		var $pages;
		function __contruct($title, $author, $page) {
		    $this->title = $name;
   			$this->author = $author;
   			$this->page = $page;
		}
	}
	$book1 = new Book("Harry Potter", "JK Rowling", 400);
?>
Object Functions
<?php 
	class Student {
		var $name;
		var $major;
		var $gpa;
		
		function __contruct($name, $major, $gpa) {
	   	 	$this->name = $name;
			$this->major = $author;
			$this->gpa = $gpa;
		}
		
		function hasHonors(){
			if($this->pga >= 3.5){
				return "true";
			}
			return "false";
		}
	}
	$student1 = new Student("Jim", "Business", 3.2);
	$student2 = new Student("Pam", "Art", 3.6);
	echo $student2->hasHonors();
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
if条件判断 if(条件表达式1,结果true和false) { 执行代码1; }else if(条件2) { 执行代码2; }else if(条件3) { 执行代码3; }else { 默认执行的执行代码; } switch多分支结构 switch(变量名称) { case 值1: 执行代码1; break; //中断程序运行,并跳到switch结束大括号}之后 case 值2: 执行代码2; break; default: 默认执行的代码; } if和switch的主要区别:if的条件应该是一个范围,switch的条件应该是一个值。 while循环语句 在什么情况下使用循环语句?重复输出一些内容时使用。 var i=1;//变量初始化; while(i<10) { document.write(i+” ”);//重复执行的代码; i++; //变量更新,避免一个死循环 } do while循环语句 do while循环是while循的一个变体。 do while循环,先执行循环体代码,再进行条件判断。至少执行一次循环体的代码。 While循环,先进行条件判断,再执行循环体的代码。如果条件不满足,直接跳转到while结束}之后 语法结构: do{ 循环的代码; }while(条件判断); //实例:输出不同等级的标题 var i = 1; var str = ""; //最后的结果 do{ str += "<h"+i+" align=\"center\">广州传智播客PHP培训学院</h"+i+">"; //str = str + …… i++;//变量更新 }while( i<=6 ); document.write(str); for循环语句 语法结构: for(变量初始化;条件判断;变量更新) { 循环体代码; } 举例1:输出1-100间所有的偶数 for(var i=1;i<100;i++) { if( i%2==0) { document.write(i+” ”); } } 数组 一、数组的基本概念 数组就是一组数据有序排列的一个集合。例如:var arr = [10,20,30,40,50] 数组元素:数组中的每一个值,就叫一个数组元素。比如:20就是一个数组元素 数组索引:数组中的第一个元素,都有一个编号(索引、下标),索引号是从0开始的正整数,依次为0、1、2…… 数组元素的访问:数组名称连上[],[]中是元素的索引号,例如:arr[4]=50,arr[0]=10 数组的长度:指数组中元素的个数 问题:数组的长度,与数组中最大索引号有什么关系?也就是:数组个数-1=数组的最大索引号 二、创建一个数组 (1)使用new运算符结合Array()构造函数来创建 方式一:创建一个未知长度的数组 var arr = new Array(); //增加数组元素,数组的值可以是任何的数据类型 //字符串、数值、布尔、undefined、null、array、object、 function arr[0] = 10; arr[1] = 20; arr[2] = true; arr[3] = "abc"; arr[4] = undefined; //相当于 arr[4]; //打印输出所有的值,通过document.write输出数组时,将自动转换成一个字符串输出 document.write(arr); 方式二:创建指定长度的数组,()中只有一个整数 var arr = new Array(3); //创建一个包含3个元素的数组 arr[0] = 10; arr[1] = 20; arr[2] = 30; 方式三:将多个数组元素添加小括号()中,各个元素间用逗号隔开 var arr = new Array(10,20,30,40,50); arr[0] = arr[0] + 90; //将第0个元素加上90 document.write(arr[0]); //结果为100 (2)使用中括号[]来创建一个数组 var arr = [10,20,30,40]; var arr = [“周列生”,true,30,“大专”,“毕业院校”];

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值