php learning notes 1

Keywords: PHP Apache

1.1 INTRODUCTION

1. Dynamic website: real-time data update dynamic (foreground, background) database

php machine language

1.2 interaction between PHP and web pages

Process: the user submits the request, the server receives the request, processes the request, obtains the data, and returns the data

1.web form operation process

<form action="0.php" method="post">

action: location of data submission
Method: submission method
{
get: simple, direct search in the search bar
post: submit with password, complex
}

example

<!DOCTYPE html>
<html>
<head>
	<title>
		php practice
	</title>
</head>
<body>
	<form name="form1" action="01.php" method="post">
		<label>Search here:</label>
		<input type="text" name="search" />
		<input type="submit" name="search_btn" value="search">
	</form>


</body>
</html>
<?php
echo $_post['search']
?>

Form security verification

1.strip_ The tags () function is used to remove HTML tags from the string
2. The htmlentities () function can convert HTML and PHP tags into characters and output them as text

<!DOCTYPE html>
<html>
<head>
	<title>
		php practice
	</title>
</head>
<body>
	<form name="form1" action="01.php" method="post">
		<label>personal information:</label>
		<br>
		<label for="username">user name:</label>
		<input type="text" name="username" />
		<br>
		<label>Student No.:</label>
		<input type="text" name="number" />
		<br>

		<input type="submit" name="sub" value="land">
	</form>
<?php
echo strip_tags($_POST['username']); //Filter tag
echo htmlentitles($_POST['pass']); //Direct output tag
?>

</body>
</html>

Form data validation

1.isset() function: used to detect whether the variable has a specific value, including 0, FALSE or an empty string, but it cannot be null
2.empty() function: used to check whether the variable has null value, including string, 0, null and false
3.in_numeric() function to detect a number or numeric string.

practice

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head >
	<title></title>
</head>
<body>
    <div style="width:400px; height:800px;text-align:center;">
	<form name="form1" action="02.php" method="post" style="border: 5px solid #e6e6fa">
		<h1>Edit profile</h1>
        <label>Nickname?</label>
        <input type="text" name="username" id="username" /><br>
        <label>Gender:</label>
        <br>
        <label>female</label>
        <input type="radio" name="sex" value="woman" />
        <label>male</label>
        <input type="radio" name="sex" value="man" />
        <br>
        <label>Email:</label>
        <input type="text" name="emil" id="emil" />
        <br>
        <label>QQ number:</label>
        <input type="text" name="qq" id="qq" />
        <br>
        <label>Personal homepage:</label>
        <input type="text" name="zhuye" id="zhuye" />
        <br>
        <label>City:</label>
        <select name="city">
        	<option value="">Not selected</option>
        	<option value="Beijing">Beijing</option>
        	<option value="Tianjin">Tianjin</option>
        	<option value="Tangshan">Tangshan</option>

        </select>

        <br>
        <label>Language skills:</label>
        <br>
        <label>HTML</label>
        <input type="checkbox" name="yuyan[]" value="html">
         <label>JavaScript</label>
        <input type="checkbox" name="yuyan[]" value="javascript">
         <label>PHP</label>
        <input type="checkbox" name="yuyan[]" value="php">
         <label>C++</label>
        <input type="checkbox" name="yuyan[]" value="c++">
        <br>
        <label>introduce oneself to:</label><br>
       <textarea name="my" rows="3" cols="20"></textarea>
        <br>
        <input type="submit" id="submit" value="Save data" name="submit" style="background-color: blue;" >
        <input type="submit" value="Refill" name="pub">

</form>


</body>
</html>

<?php 
if (isset($_POST['submit'])) 
echo 'Submitted successfully!!';
echo "<br>";
$username=$_POST['username'];
if (!isset($_POST['username']) || $_POST['username']==='') {
	echo "You must enter a nickname!!";
	exit;
}
$sex=$_POST['sex'];
$emil=$_POST['emil'];
if (!isset($_POST['emil']) || $_POST['emil']==='') {
	echo "Mailbox cannot be empty!!";
	exit;
}
$qq=$_POST['qq'];
if (!is_numeric($_POST['qq']) || $_POST['qq']==='') {
	echo "QQ Cannot be empty and must be a number!!!";
	exit;
}
$zhuye=$_POST['zhuye'];
$city=$_POST['city'];
$yuyan=$_POST['yuyan'];
$my=$_POST['my'];
echo "Nickname?" .$username ."<br />";
echo "Gender:" .$sex ."<br />";
echo "Email:" .$emil ."<br />";
echo "QQ: " .$qq ."<br />";
echo "Personal homepage:" .$zhuye ."<br />";
echo "city:" .$city ."<br />";
echo "Language skills:" .implode(',', $yuyan) ."<br />";
echo "introduce oneself to:" .$my ."<br />";
 ?>

2.1 PHP basic syntax

2.1.1 PHP Tags

1. Standard mark <? php ?>
2. Short mark <? >
3.ASP tag <%% >
4.Script tags

<script language="php"></script>

##2.1.2 notes

<?php
echo "hello world"; //notes
/*
multiline comment 
*/
?>

2.1.3 PHP identifier

1. Define identifier rules
(1) However, any length can only be composed of letters, numbers and underscores.
(2) Cannot start with a number
(3) Cannot contain spaces
(4) If identifiers consist of multiple words, they should be separated by underscores
user_name

2.1.4 PHP constants

1. Declaration of constants

bool define(string $name,mixed $value [,bool $case_insensitive=false])

2. In the constant declaration, the parameter n a m e and name and name and value are required. They are the names and values used to specify constants, respectively
Parameter $case_insensitive is optional and is used to specify whether constant names are case sensitive
If true, the parameter is called. Otherwise, it is case insensitive. Otherwise, it is case sensitive. The default value is false

<?php
//Define a constant named ip. true indicates that the constant is case insensitive
define("ip", "hello",true);
echo ip;//Output constant
//Define a constant named ty. The default constant is case insensitive
define("ty", "hello",true);
?>

php variable

1. Definition of variables:

<?php
//Define variables
$text;
$number;
$ABC_123;
$_book;
//Declaration without display
$number=10; //The variable $number is defined and assigned a value of 10
$result=$nubmer;//Define the variable $result and assign the value of number to result
echo $number;//Output variable
echo "<br>";
echo $result;//Output variable

2. Reference and assignment of variables

<?php
$txt='hello';
$new_txt=&$txt;  //$new_txt refers to $txt, and then the values of the two variables change at the same time
?>

2.1.6 data types of PHP

Standard types: Boolean, integer, float, string, string
Compound type array object object object resource resource resource type null null null

Data type of detection variable: use is_* () function, (variable to be detected) matches and returns true

<?php
$a=null;
echo 'Check whether it is empty:'.is_null($a);
?>

2.1.7 PHP automatic type replacement

1. Convert to Boolean
When c is Boolean, some values of you've will turn to false
(1) Integer 0 (2) floating point value 0.0 (3) empty string, and string. "0", (4) array without any elements (5) object without any member variables
2. Convert to integer
(1) Boolean to integer: true to 1, false to 0
(2) Floating point to integer conversion: floating point numbers are rounded down when converted to integers
(3) Convert string to integer: the beginning of the string determines its value (legal value (optional positive and negative)
Sign, followed by one or more numbers, followed by an optional index
. otherwise, 0
E. e will be used as float value, otherwise it is an integer

<?php
$a=true;
$b=$a+1; //Boolean numbers are added to integers
var_dump($b);//Output as int (2)
//Convert character type to integer type, and 1.5e2 is expressed as 1.5 * (10 * 10)
$char1=1+"-1.5e2";//Add character and integer numbers
var_dump($char1);//float(-149)
$char2=1+"char";//Add character and integer numbers
var_dump($char2);//int(1)
$char3=1+"10char";
var_dump($char3);//int(11)

?>

3. Convert to string type
(1) Boolean to string: true is "1" false is null character ""
(2) Convert integer and floating-point to string: convert the literal style of numbers to string style

<?php
$a=true;
echo $a;
$b=3;
$e=3.3;
$c=$b.'string'.$e;
var_dump($c);
$num="12787545abc10";
var_dump($num);
$str="123";
var_dump((integer)$str);
?>

2.1.8 PHP cast

If (integer) is forcibly converted to integer, add a parenthesis before the variable and fill in the target type in parentheses.

Posted by eric_e on Wed, 29 Sep 2021 22:02:50 -0700