PHP programming

77 43 0
PHP programming

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

PHP PHP is one of the most widespread and effective programming languages for the WEB and it is also very simple to understand Moreover, we can find many free software to make the code we write run The file to program in php must always have php extension, and it also supports all HTML tags This text is aimed at beginners, long and complex examples are not used in order to learn faster without detracting from the effectiveness of what you learn and also makes sure that even the explanations are not long and boring doing so the reader is put in a position to learn directly by programming, since I think that programming as everything is learned by practicing it All the examples in this text have been done to create safe and error-free examples INDEX Variables Arithmetic operators Comparison operators Instruction if Instruction if else Array The increment operator The decrease operator The for cycle 10 The while loop 11 Instruction break 12 Instruction continue 13 Take the values from a form 14 Instruction switch 15 Logical operators 16 Instruction exit 17 The rand function 18 Pick up the date from the server 19 Verify a numeric value 20 The function strlen 21 The foreach cycle 22 Instruction empty 23 Functions 24 Static Variables 25 The constants 26 Create a folder 27 Delete a folder 28 Take the IP address 29 Write and read files 30 Check to see if a file exists 31 Delete a file 32 Upload file 33 Session variables 34 Cookie 35 Turn characters into lowercase 36 Turn characters into uppercase 37 Format a number 38 Look for a value in an array 39 Instruction die 40 Interact with databases 41 Look for a value in a tables of a DB 42 Insert a record 43 Change a record 44 Delete a record 1) Variables Like all programming languages we find the variables, which are entities in which we will store our data as numbers, alphanumeric characters, etc In PHP to declare a variable, just precede it with the $ symbol Example: We have assigned to the variable Name the string Mario, if we make this piece of code we see that the string will be printed The strings must be enclosed in double quotes as in the example Example 2: With the second example, we declared a variable and assigned it the value of 20, then with the echo function we had it printed on screen As for the assignment of a value to a variable, we can also declare a variable and then assign it a value later Example: 2) Arithmetic operators The arithmetic operators as we all know are addition, subtraction, multiplication and division and module Let's see what the symbols are and how they are used in PHP Sum of two numbers: Division of two numbers: Subtraction of two numbers: Multiplication of two numbers: Module between two numbers: In this case will be returned 3) Comparison operators The comparison operators are used to compare two values and to see if they are equal or different in this case see also which is the major or the minor Their symbols are: Minor < Minor or equal Greater or equal >= Different != To check if two values are the same, the operator is double the same, to be careful because at the beginning one is often confused So how can we easily guess if we have two numbers such as and 12 if we write: = true As for the comparison the same if we write == will be true From how we see we assign a null string to the data and then set the time with a negative value 35) Turn characters into lowercase To transform a string of characters all in lower case we use the strtolower () function Example 36) Turn characters into uppercase To transform a string of uppercase characters we use the strtoupper () function Example 37) Format a number To format a number use the function number_format () which in the brackets insert arguments, the first is the number to be formatted, the second represents the number of decimal places, the third is the decimal separator and the fourth separates every three digits of the whole part Example: In this case the function returns the number so formatted 2,300,000.00, so that not only is it more professional to present a number as written above, but above all it is more readable 38) Look for a value in an array If we want to verify the existence of a value in an array we can use the in_array () function We can make an example directly as it is a very simple instruction to use 39) Instruction die The die instruction is similar to exit, so when we want to interrupt the flow of the program we can use that instruction 40) Interact with databases Now that we have learned a sufficient amount of commands to create a website, in this last part we see how to interact with a MySQL database, and then add, modify, delete and read data from a table of a DB We begin to create a table in MySQL called Subscribers, as fields we enter ID, name, surname, city (we write city without accent) and insert fill some records so that in the first example we will see how to connect to a database and pull out some data Done when said, in the code that follows we set up a connection to the DB and then we go to give some examples on how to get the data out For those who have already been able to execute instructions in SQL everything will be easier, but also for others it is not difficult at all Then: In the first line I connect to the server by passing three parameters that are localhost, then enter the username and password that we have set In the second line we insert the name of the Database, and the connection to the server, while in the third line is an SQL statement that selects all the fields from the registered table, and that string is passed to the mysql_query function with the respective connection Or die etc we insert it in such a way that if an error occurs we will notify and end the flow, if everything is OK, let's proceed Then in the variable $ numrows we insert the number of rows and then check that it is non zero because if it is zero the table is empty, and so if it is not empty we continue At this point, through a cycle, we scroll through the whole table, taking the data 41) Look for a value in a tables of a DB We can the research for any field, for example, looking for a registered name god, Marco, or for his surname Rossi, or for ID Usually in searches it is obvious to a search for a field that is unique such as the tax code or the ID field, while if I a search by name in the result I will return all those with the same name, such as if I a searching in a table to find a car, it's better to it for a license plate so that you can look for only what I want In our example, to look for a subscriber in the table just change the SQL statement in the string $ query, in fact we try to write in the code that we wrote the following: $query=”Select * from iscritti where nome='Marco' “ All the members named Marco will come out Example:

Ngày đăng: 05/03/2019, 08:49

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan