PHP Learning Note One

Source: Internet
Author: User

PHP Learning Notes

format: <?php?>

Output: 1. Echo outputs a string

2. Var_dump () output array

Variable:

$ start;

Global variables, local variables (within functions);

If a global variable is to be used within a function, the global variable must first be declared; (Global $connection; declare global variables;)

Array :

format: 1. $a = [1,2,3,4];

2. $a = array (7,9,8,6);

Array output:

Var_dump ($a);

The length of the array:

1.count ($a)

2.sizeof ($a)

Traversal of the array:

foreach ($a as $key = = $value) {

echo $key. ': ' $value. ' <br> ';

}

$key: index, $value: value

Determine if an array has a value:

In_array (' value ', array );

Array de-weight:

Array_unique ();

File operations

File read/write

$_post[ The name of the form field ],$_get[ The name of the form field ] name of the form field =name Value

Read file file_get_contents ( file address )

Write file file_put_contents (file name, content, Flag )

do not write the flag each time overwrite, append with File_append;

eg: Message Board

New.html

<form action= "input-file.php" method= "POST" >

<p> Username: <input type= "text" name= "member" ></p>

<p> Password: <input type= "password" name= "pwd" ></p>

<p><textarea name= "txt" id= "cols=" "rows=" ></textarea></p>

<p><input type= "Submit" value= ' submission ' ></p>

</form>

input_file.php

<?php

$member = $_post[' member ');

$pwd = $_post[' pwd '];

$txt = $_post[' txt '];

Var_dump ($member, $pwd, $txt)

File_put_contents (' Data.txt ', $member. ', '. $pwd. ', ' $txt. ' <\n> ', file_append);

echo "OK <a href= ' new.html ' > Continue adding </a> <a href= ' show.php ' > View all </a>"

?>

show.php

<?php

Read File file_get_contents ()

$message = file_get_contents (' data.txt ');

Echo $message;

String substitution str_replace ();

echo str_replace (' \ n ', ' <br> ', $message);

echo "<p><a href= ' new.html ' > Continue adding </a></p>"

?>

Open File

<?php

$file = fopen ("Test.txt", "R") or exit ("Unable to open file!"); Open File

feof ($file) file ends

while (!feof ($file))

{

Echo fgets ($file). "<br>"; Read each line of the file until the end of the file

echo fgetc ($file). "<br>"; To read a file verbatim

}

Fclose ($file); Close File

?>

Function

function () {

}

Arguments are used to use variables when passing parameters

function ($a) {

echo $a; (return $a)

}

Database

Connecting to a database

MySQL host : $host

MySQL user name : $user

MySQL password : $password

Mysql_connect ($host, $user, $password);

Mysqli

manipulating databases

MySQL database name : $dbname

MySQL connection identifier : $connection = mysql_connect ($host, $user, $password)

if (! $connection) {

Exit (' connection failed ');//Die ()

} else {

echo " connected successfully ";

}

mysql_select_db ($dbname, $connection)

PHP Close Database connection

Mysql_close ($connection)

MySQL query statement :

$sql

mysql_query ("SET NAMES UTF8"); Coding: Preventing garbled characters

mysql_query ($sql)

Add : Insert

Delete: delete

Change : Update

Check : Select

Read Print

Mysql_fetch_array ()

Debugging

Ini_set (' display_errors ', ' on ');

Error_reporting (E_all); Turn On Error debugging

PHP Learning Note One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.