课程
本课程为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();
?>