php笔记

phpinfo();

echo "hello world!";

注释:

//
#
/*  */

双引号里面的变量会被替换

echo "{$my_variable} Again."

$array1 = array(4,5,6,7,8,9);
echo $array1[0];

$array2 = array(6, "forx", array("x", "y"));
echo array2[3][1];
$array2[3] = "cat"
echo $array2[3];

$array3 = array("first_name" => "Kevin", "last_name" => "test")
echo $array3["first_name"]

print_r($array2);



in_array(3, $array);

常量:

define("MAX_WIDTH", 980)
echo MAX_WIDTH;

foreach($array as $age) {
    echo $age
}

foreach($array as $position => $age) {
    
}

$array = array("computer"=>2000, "computer"=>2000)
foreach($array as $key => $value) {
    
}

echo "1: " . current($ages)
next($age)
echo "2: " . current($ages)

while($age = current($ages)) {
   echo $age;
   next($ages);
}

function say_hello() {
   echo "test"
}

say_hello();

function addition($first, $second) {
   $sum = $first + $second;
   return $sum;
}

function addition($first, $second) {
   $sum = $first + $second;
   $subt = $first - $second;
   $result_array = array($sum, $subt);
   return $result_array;
}

print_r($_GET);
$id = $_GET['id'];
echo $id;

<?php urlencode("kevin&"); ?>

这个可以防止恶意用户 直接写html标签
htmlspecialchars($linktext);


$url = "http://localhost/";
$url .= rawurlencode($url_page);
$url .= "?param1=" . urlencode("$param1");
$url .= "&param2=" . urlencode("$param2");

$username = $_POST["username"];

setcookie("test", 45, time()+(60*60*24*7));

if(isset($_COOKIE['test'])) {
  $var1 = $_COOKIE['test'];
  echo $var1;
}


session_start();

$_SESSION['name'] = "kenvin";

返回404
header("HTTP/1.0 404 NOT FOUND");
exit;

跳转到其他页面(客户端)
header("Location: basic.html");
exit;

include("included_func.php");
hello();

included_func.php:
<?php
   function hello() {
      echo "hello";
   }
?>

requere("included_func.php");
找不到报错

requere_once("included_func.php");

1. Create a database connection
$connection = mysql_connect("localhost", "root", "ps");
if(!$connection) {
  die("Database connection failed: " . mysql_error());
}

2. select a database to use
$db_select = mysql_select_db("widget_corp", $connection);
if(!$db_select) {
  die("Database selection failed: " . mysql_error());
}

3. Perform database query
$result = mysql_query("SELECT * FROM subject", $connection);
if(!$result) {
  die("Database query failed: " . mysql_error());
}

4. Use returned data
while($row = mysql_fetch_array($result)) {
    echo $row['menu_name'] . " " . $row['position']
}

5. Close connection
mysql_close($connection);


你可能感兴趣的:(PHP)