text [PHP] PHP Basic
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text [PHP] PHP Basic相关的知识,希望对你有一定的参考价值。
# OOP stats for Object Orientet Programming
# Creat a class
class Person{
# Properties
# Php has three access modifier (public, privat, protected)
private $name;
private $email;
# Create a constructor
public function __construct($name, $email){
$this->name = $name;
$this->email = $email;
}
# Destruct a class
public function __destruct(){
}
# Getters and setters
public function getName(){
return $this->name;
}
public function setName($name){
$this->name = $name;
}
public function getEmail(){
return $this->email;
}
public function setEmail($email){
$this->email = $email;
}
}
# The filesystem functions allow you to access an manipulate the filesystem
# basename - return filename
echo basename('/dir/file.php');
# basename - return filename without extension
echo basename('/dir/file.php', '.php');
# dirname - return the direcoryname form path
echo dirname('/dir/file.php');
# file_exists - checks if a file exists
echo file_exists('file.txt');
# realpath - get the absolute path
echo realpath('file.txt');
# is_file - checks to see if file
echo is_file('file.txt');
# is_writable - check if file is writable
echo is_writable('file.txt');
# is_readable - check if file is readable
echo is_readable('file.txt');
# filesize - get the file size
echo filesize('file.txt');
# mkdir - create a directory
mkdir('dir');
# rmdir - delete directory if empty
rmdir('dir');
# copy - copy a file
copy('file.txt', 'file1.txt');
# rename - rename a file
rename('file.txt', 'myfile.txt');
# unlink - delete a file
unlink('file.txt');
# file_get_contents - write form file to string
echo file_get_contents('file.txt');
# file_put_contents - write string to file
file_put_contents('file.txt', 'Hello World');
# fopen - create and open file
$handle = fopen('file.txt', 'r');
# fread - read file
fread(fopen('file.txt', 'r'), filesize('file.txt'));
# fwrite -write file
fwrite(fopen('file.txt', 'w'), 'test');
# fclose - close file
fclode(fopen('file.txt', 'r'));
# With shorthands you can shorthand if statment
# Short if statment
$loggedIn = true;
echo ($loggedIn) ? 'You are logged in' : 'You are NOT logged in';
# Short nested if statment
$age = 20;
$score = 10;
echo 'Your score is: '
.($score > 10 ?
($age > 10 ? 'Average' : 'Exceptional'):($age > 10 ? 'Horrible' : 'Average')
);
# This are the diffrent fuction for strings
# substr - returns a portion of a string
$output = substr('Hello', 1);
echo $output;
$output2 = substr('Hello', 1, 3);
echo $output2;
$output3 = substr('Hello', -2);
echo $output3;
# strlen - returns the length of a string
$output4 = strlen('Hello World');
echo $output4;
# strpos - finds the position of the first occurence of a sub string
$output5 = strpos('Hello World', 'o');
echo $output5;
# strrpos - finds the position of the last occurence of a sub string
$output6 = strrpos('Hello World', 'o');
echo $output6;
# trim - strips whitespace
$output7 = trim('Hello World ');
echo $output7;
# strtoupper - makes everything uppercase
$output8 = strtoupper('Hello World!');
echo $output8;
# strtolower - makes everything lowercase
$output9 = strtolower('Hello World!');
echo $output9;
# ucwords - capitalize every word
$output10 = ucwords('hello world');
echo $output10;
# str_replace - replace all occurences of a search string with a replacement
$output11 = str_replace('World', 'Everyone', 'Hello World!');
echo $output11;
# is_string - check if string
$val = 'Hello';
$output12 = is_string($val);
echo $output12;
# gzcompress - compress a string
# gzuncompress - uncompress a conpressed string
$string = 'lasdkjfoewjf sdljewojsflhglksjsedoghsgljrflkjellkjfweoidlkajsfjwj';
$compressed = gzcompress($string);
echo $compressed;
$original = gzuncompress($compressed);
echo $original;
// Superglobals are built-in variables that are always available in all scopes
// There are nine superglobals in php
// $GLOBALS - References all variables available in global scope
// $_SERVER - Server and execution enviroment information
echo $_SERVER['SERVER_NAME'];
// $_GET - HTTP GET varibales
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
// $_POST - HTTP POST varibales
echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
// $_FILE - HTTP File upload variables
$upload = diverse_array($_FILES["upload"]);
// $_COOKIE - HTTP Cookies
echo 'Hello ' . htmlspecialchars($_COOKIE["name"]) . '!';
// $_SESSION - Session varibales
session_start();
$_SESSION['test'] = 42;
echo $_SESSION['test'];
// $_REQUEST - HTTP Request variables
$_GET['foo'] = 'a';
$_POST['bar'] = 'b';
var_dump($_GET);
var_dump($_POST);
var_dump($_REQUEST);
// $_ENV - Environment variables
echo 'My username is ' .$_ENV["USER"] . '!';
// The Date function can be used to display dates and times
// To get the current date
echo date('d');
// To get the current month
echo date('m');
// To get the current year
echo date('Y');
// To get the current day of the week
echo date('l');
// To get the current houre
echo date('h');
// To get the current minutes
echo date('i');
// To get the current seconds
echo date('s');
// To get am or pm
echo date('a');
// To set the time zone use the fuction date_default_timezone_set
date_default_timezone_set('America/New_York');
// To get the current time zone use the function date_dafault_timezone_get
echo date_default_timezone_get();
// To make a Date and Time use the function mktime it takes in six parameter (hour,min,sec,mouth,day,year)
$timestamp = mktime(10, 14, 54, 10, 27, 1994);
echo date('Y/m/d', $timestamp);
// To convert a string in to time use the strtotime function
$timestamp2 = strtotime('7:00pm March 22 2018');
$timestamp3 = strtotime('tommorrow');
$timestamp4 = strtotime('next Sunday');
$timestamp5 = strtotime('+2 Months');
// To convert a timestamp in to a readable date use the date function
echo date('Y/m/d', $timestamp2);
echo date('Y/m/d', $timestamp3);
echo date('Y/m/d', $timestamp4);
echo date('Y/m/d', $timestamp5);
// Comparision are used to compair two values
// Ther are eight types of comparison operators (==, ===, <, >, <= , >=, !=, !==)
// Ther are three types of logical operators (and &&, or ||, xor)
// Equal operator (==)
$num = 5;
if($num == 5){
echo 'The value num is equals to 5';
}else{
echo 'Tha value num is not equals to 5';
}
// Identical operator (===)
$num2 = 5;
if($num2 === 5){
echo 'The value num2 is identical to 5';
}else{
echo 'Tha value num2 is not identical to 5';
}
// Greater than operator (>)
$num3 = 5;
if($num3 > 5){
echo 'The value num3 is greater than to 5';
}else{
echo 'Tha value num3 is not greater than to 5';
}
// Less than operator (<)
$num4 = 5;
if($num4 < 5){
echo 'The value num4 is less than to 5';
}else{
echo 'Tha value num4 is not less than to 5';
}
// Greater than or equals operator (>=)
$num5 = 5;
if($num5 >= 5){
echo 'The value num5 is greater than or equals to 5';
}else{
echo 'Tha value num5 is not greater than or equals to 5';
}
// Less than or equals operator (<=)
$num6 = 5;
if($num6 <= 5){
echo 'The value num6 is less than or equals to 5';
}else{
echo 'Tha value num6 is not less or equals than to 5';
}
// Not equal operator (!=)
$num7 = 5;
if($num7 != 5){
echo 'The value num7 is not equals to 5';
}else{
echo 'Tha value num7 is equals to 5';
}
// Not identical operator (!==)
$num8 = 5;
if($num8 !== 5){
echo 'The value num8 is not identical to 5';
}else{
echo 'Tha value num8 is identical to 5';
}
// And operator (and &&)
$num9 = 5;
if($num9 > 5 and $num9 < 10){
echo 'The value num9 greater than 5 and than to 10';
}
// Or operator (or ||)
$num10 = 5;
if($num10 > 5 or $num10 > 10){
echo 'The value num10 greater than 5 or than to 10';
}
// Xor operator (xor)
$num10 = 5;
if($num10 > 5 xor $num10 > 10){
echo 'The value num10 greater than 5 or than to 10';
}
// Conditsonals are used to test for a value
// If statment
$num = 5;
if($num == 5){
echo 'The value num is equals to 5';
}
// If else statment
$num2 = 5;
if($num2 == 5){
echo 'The value num2 is equals to 5';
}else{
echo 'Tha value num2 is not equals to 5';
}
//if elseif else statment
$num3 = 5;
if($num3 == 5){
echo 'The value num3 is equals to 5';
}elseif($num == 6){
echo 'The value num3 is equals to 6';
}else{
echo 'Tha value num3 is not equals to 5';
}
// Nesting if
$num4 = 5;
if($num4 > 5){
if($num4 < 10){
echo 'The value num4 greater than 5 and than to 10';
}
}
// Switch statment
$favColor = 'red';
switch($favColor){
case 'red':
echo 'Your facorite color is red!';
break;
case 'blue':
echo 'Your facorite color is blue!';
break;
case 'green':
echo 'Your facorite color is green!';
break;
default:
echo 'Your facorite color is something else';
}
// A function is a block of code that can be repeatedly called
// Basic function
function simpleFunction(){
echo 'Hello World';
}
simpleFunction();
// Function with Parameters
function sayHello($name){
echo "Hello $name<br>";
}
sayHello('Raphael');
// Function with default value for the Parameter
function sayHello2($name => 'World'){
echo "Hello $name<br>";
}
seyHello2();
// Return Value
function addNummbers($num1, $num2){
return $num1 + $num2;
}
echo addNummber(2,3);
// Passing Arguments by Refrence
$myNum = 10;
function addFive($num){
$num += 5;
}
function addTen(&$num){
$num += 10;
}
addFive($myNum);
echo "Value $myNum<br>";
addTen($myNum);
echo "Value $myNum<br>";
// Loops execute code set number of times
// There are four types of loops (for, while, do..while, foreach)
// For loop
// For loops have the following parameters (initializer, condition, increment)
for($i = 0; $i < 10; $i++){
echo $i;
echo '<br>';
}
// While loop
// While loops have the following parameters (condition)
$l = 0;
while($l < 10){
echo $l;
echo '<br>';
$l++;
}
// Do...while loop
// Do...while loops have the following parameters (condition)
$j = 0;
do{
echo $j;
echo '<br>';
$j++;
}while($j < 10);
// Foreach loop
// The foreach loop is used to go thru a array
$ints = array(1,2,3);
foreach($ints as $int){
echo $int;
echo '<br>';
}
$people = array("Raphael" => "test@test.ch", "Maya" => "mail@mail.ch", "Paul" => "paul@dev.com");
foreach($people as $person => $email){
echo $person .': '.$email;
echo '<br>';
}
// An array is a variable that can hold multible values
// There are three types of array (indexed, associative, mulit-demensional)
// Arrays are zero based
// Indexed array
$people = array('Kevin', 'Jeremy', 'Sara');
echo $people[1];
$ids = array(1,2,3);
echo $ids[0];
$cars = ["Honda", "Toyota", "Ford"];
echo $cars[2];
// Adding to a indexed array
$cars[3] = "Chevy";
$cars[] = "BMW";
// Associative array
$people2 = array("Brad" => 35, "Raphael" => 23, "Maya" => 60);
echo $people2["Brand"];
$ids2 = [35 => "Brand", 23 => "Raphael", 60 => "Maya"];
echo $ids2[23];
// Adding to a associative array
$people2["Yannick"] = 21;
echo $people2["Yannick"];
// Multi-dementional array
$cars2 = array(
array("Honda", 20, 10),
array("Toyota", 15, 5),
array("BMW", 25, 0)
);
echo $cars[1][0];
// To get the nummber of values in a array use the count function
echo count($cars);
// To show the hole array use the print_r function
echo print_r($cars);
// To show the data types of the values in the array use the var_dump function
echo var_dump($cars);
// Constanst cant change
// To create a constans use the define function
define("GREETING", "Hello Everyone");
echo GREETING;
// To make a constans non case sensitiv add a third parameter to the defind function
define("GREETING2", "Hello Everyone", true);
echo greeting2;
// Variables must have a $ as prefix
// Variables need to start with a letter or a underscore
// Variables can contain only letters, nummbers and undersores
// Variables are case sensitve
// Variables of the type string are in between ""
$var = "Hello World";
echo $var;
// Variables of the type integer and float did not have a "" around it
$var2 = 2;
echo $var2;
// Variables of the type boolean can be ether true or false
$var3 = false;
echo $var3;
// To concatened two variables use ether a . or "" around it
$var4 = "Hello";
$var5 = "World";
$res = $var4 . $var5;
echo $res
$res2 = "$var4 $var5!";
echo $res2
// To calculate two integer or float variables together use a +,-,*,/ sight
$var6 = 10;
$var7 = 4;
$add = $var6 + $var7;
echo $add;
$sub = $var6 - $var7;
echo $sub;
$mul = $var6 * $var7;
echo $mul;
$dev = $var6 / $var7;
echo $dev;
// To echo out a special character add a \ before the character
echo 'They\'re Here'
//Ther are two ways to print out content in PHP
echo "Hello World";
print "Hello World";
$string = "Hello World!"; //String
$int = 1; //Integer
$float = 1.2; //Float
$booleant = true; //Booleant
$array = [1,2,3]; //Array
$object = new Object(); //Object
$null = null; //NULL
// This is a Singleline comment
# This is a Singelline comment
/* This is a Multiline comment */
以上是关于text [PHP] PHP Basic的主要内容,如果未能解决你的问题,请参考以下文章
PHP 模拟 HTTP 基本认证(Basic Authentication)
如何从php服务器端向像Android这样的Visual Basic客户端发送通知