Hướng dẫn dùng increase increment trong PHP

PHP for Loop


The for loop - Loops through a block of code a specified number of times.

Nội dung chính

  • PHP for Loop
  • The PHP for Loop
  • Example Explained
  • Example Explained
  • PHP Exercises
  • Examples in Each Chapter
  • SQL Exercises
  • Test Yourself With Exercises
  • SQL Examples
  • SQL Quiz Test
  • My Learning
  • SQL References
  • SQL Data Types
  • Kickstart your career
  • Call By Value
  • Call by Reference
  • Call by Value & Call by Reference in PHP
  • Call by Value in PHP
  • Call by Reference in PHP
  • Swap Two Numbers using Call By Value in PHP
  • Swap Two Numbers using Call By Reference in PHP
  • What is call by value and call by reference PHP?
  • What is difference between call by value and call by reference?
  • What is call by reference in PHP?
  • What is call by value and call by reference explain with example?


The PHP for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for (init counter; test counter; increment counter) {
  code to be executed for each iteration;
}

Parameters:

  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value

Examples

The example below displays the numbers from 0 to 10:

Example

for ($x = 0; $x <= 10; $x++) {
  echo "The number is: $x
";
}
?>

Try it Yourself »

Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 10; - Continue the loop as long as $x is less than or equal to 10
  • $x++ - Increase the loop counter value by 1 for each iteration

This example counts to 100 by tens:

Example

for ($x = 0; $x <= 100; $x+=10) {
  echo "The number is: $x
";
}
?>

Try it Yourself »

Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 100; - Continue the loop as long as $x is less than or equal to 100
  • $x+=10 - Increase the loop counter value by 10 for each iteration


PHP Exercises

SQL is a standard language for storing, manipulating and retrieving data in databases.

Our SQL tutorial will teach you how to use SQL in: MySQL, SQL Server, MS Access, Oracle, Sybase, Informix, Postgres, and other database systems.

Start learning SQL now »


Examples in Each Chapter

With our online SQL editor, you can edit the SQL statements, and click on a button to view the result.

Click on the "Try it Yourself" button to see how it works.


SQL Exercises

Test Yourself With Exercises

Exercise:

Insert the missing statement to get all the columns from the Customers table.

Start the Exercise



SQL Examples

Learn by examples! This tutorial supplements all explanations with clarifying examples.

See All SQL Examples


SQL Quiz Test

Test your SQL skills at W3Schools!

Start SQL Quiz!


My Learning

Track your progress with the free "My Learning" program here at W3Schools.

Log into your account, and start earning points!

This is an optional feature, you can study W3Schools without using My Learning.

Hướng dẫn dùng increase increment trong PHP


SQL References

At W3Schools you will find a complete reference for keywords and function:

SQL Keyword Reference

MYSQL Functions

SQLServer Functions

MS Access Functions

SQL Quick Reference


SQL Data Types

Data types and ranges for Microsoft Access, MySQL and SQL Server.

SQL Data Types


Kickstart your career

Get certified by completing the course

Get certified

w3schoolsCERTIFIED.2022
  • #1

AUto increment là 1 tính năng rất phổ biến trong sql nó được sử dụng hầu hết cho tất cả các bạn để tăng ID 1 cách tự động dễ dàng trong việc insert cũng như delete dữ liệu.
Khi các bạn tạo bảng trong phpmyadmin các bạn có thể tạo bằng query thì việc thêm Auto_increment khá dễ dàng ví dụ mẫu như sau:

CREATE TABLE User(
ID int NOT NULL AUTO_INCREMENT,
Username varchar(255) NOT NULL,
Email varchar(255),
);

Tuy nhiên khi bạn tạo bảng bằng tính năng có sẵn trong phpadmin có thể không để ý tính năng auto_increment ở đâu vì nó viết tắt. KHi các bạn tạo bảng thì check vào phần A_I như hình phía dưới đây để bật tính năng này lên.

Còn trong trường hợp bạn đã tạo rồi thì có sửa bằng cách chạy lệnh hoặc sửa tương tự. Bằng cách vào bảng đó, sau đó click vào Sructure và chọn Change

Và giống như phần lúc tạo, đánh dấu vào mục A_I để chọn auto increment cho cột đó.

Ngoài ra các bạn cũng có thể thiết lập giá trị auto_increment bằng cách chọn vào Operatioons -> Điền số vào phần AUTO_INCREMENT sao cho hợp lý hoặc reset lại tù đầu bằng cách điền số 1 vào.

  • Chủ đề mysql phpmyadmin
  • In case of PHP call by reference, actual value is modified if it is modified inside the function. In such case, you need to use & (ampersand) symbol with formal arguments. The & represents reference of the variable.

    Nội dung chính

    • Help Others, Please Share
    • Call By Value
    • Call by Reference
    • Call by Value & Call by Reference in PHP
    • Call by Value in PHP
    • Call by Reference in PHP
    • Swap Two Numbers using Call By Value in PHP
    • Swap Two Numbers using Call By Reference in PHP
    • What is call by value and call by reference PHP?
    • What is difference between call by value and call by reference?
    • What is call by reference in PHP?
    • What is call by value and call by reference explain with example?

    Let's understand the concept of call by reference by the help of examples.

    Example 1

    In this example, variable $str is passed to the adder function where it is concatenated with 'Call By Reference' string. Here, printing $str variable results 'This is Call By Reference'. It is because changes are done in the actual variable $str.

    Output:

    This is Call By Reference
    

    Example 2

    Let's understand PHP call by reference concept through another example.

    Output:


    For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to [email protected]






    Call By Value

    In this method, only values of actual parameters are passing to the function. So there are two addresses stored in memory. Making changes in the passing parameter does not affect the actual parameter.

    Example

    1. function increment($var){  
    2.     $var++;  
    3.     return $var;  
    4. }  
    5. $a = 5;  
    6. $b = increment($a);  
    7. echo $a  
    8. echo $b  
    9. ?>   

    Call by Reference

    In this method, the address of actual parameters is passing to the function. So any change made by function affects actual parameter value.

    Example

    1. function increment(&$var){  
    2.     $var++;  
    3.     return $var;  
    4. }  
    5. $a = 5;  
    6. $b = increment($a);  
    7. echo $a  
    8. echo $b  
    9. ?>  

    What’s the difference between call by value & call by reference in PHP? This is the most important concept used in programming and sometime asked in interviews. Don’t worry if you are new to this terminology, you’ll learn this concept in this tutorial.

    Call by value & call by reference in PHP

    Call by Value & Call by Reference in PHP

    Example 1: Let’s understand this concept using some programming examples.

    /**

    * Passed value using call by value

    */

    functionincrement($num)

    {

    $num=$num+1;

    echo $num."\n";

    }

    $n=1;

    increment($n);/* Output 2 */

    echo$n;/* Output 1 */

    Example 2: Now again write this script with minimal modification in an argument.

    /**

    * passed value using call by reference.

    */

    functionincrement(&$num)

    {

    $num=$num+1;

    echo$num."\n";

    }

    $n=1;

    increment($n);/* Output 2 */

    echo$n;/* Output 2 */

    We have seen the examples and their differences in output. Let’s understand what exactly is call by value & call by reference.

    PHP MCQ for practice

    Call by Value in PHP

    In call by value, the value of a variable is passed directly. It means, if the value of a variable within the function is changed, it doesn’t get changed outside of the function. As we seen in our first example.

    Call by Reference in PHP

    In call by reference, the address of a variable (their memory location) is passed. In the case of call by reference, we prepend an ampersand (&) to the argument name in the function definition. Any change in variable value within a function can reflect the change in the original value of a variable which we have seen in our second example.
    Magic methods in PHP

    Swap Two Numbers using Call By Value in PHP

    In call by value, the actual value of a variable is passed. In this example, we have swap two numbers using call by value.

    Swap two numbers in PHP

    PHP code – Find first non-repeated character in a string

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    /**

    * Swap two numbers using call by value

    * @param type $first

    * @param type $second

    */

    functionswap($first,$second)

    {

    $temp= $first;

    $first=$second;

    $second=$temp;

    echo"First number ".$first." Second number ".$second."\n";

    }

    $a=5;

    $b=7;

    /* Output : First number 7 Second number 5 */

    swap($a, $b);

    /*

       Output : First number 5 Second number 7

       original value is not changed even after calling swap method.

    */

    echo"First number ".$a." Second number ".$b;

    Swap Two Numbers using Call By Reference in PHP

    In call by reference,  Address of a variable is passed.  Address represents where the value of a variable is stored. In this example, we swap two numbers using call by reference in PHP.

    How to sort a string in PHP

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    /**

    * Swap two numbers using call by reference

    * @param type $first

    * @param type $second

    */

    functionswap(&$first,&$second)

    {

    $temp =$first;

    $first=$second;

    $second=$temp;

    echo"First number ".$first." Second number ".$second."\n";

    }

    $a=5;

    $b=7;

    /* Output : First number 7 Second number 5 */

    swap ($a,$b);

    /* Output : First number 7 Second number 5 */

    echo"First number ".$a." Second number ".$b;

    In the above example, we have passed the value of $a = 5 and $b = 7 in swap() method. In swap() method, we are passing the value by reference. It means the address of a variable is passed. So any change in the value of variables in a swap() will reflect them in original values of a variable.

    PHP 7 features

    For further you can check PHP documentation.

    What is call by value and call by reference PHP?

    Call by value means passing the value directly to a function. The called function uses the value in a local variable; any changes to it do not affect the source variable. Call by reference means passing the address of a variable where the actual value is stored.

    What is difference between call by value and call by reference?

    In the Call by Value method, there is no modification in the original value. In the Call by Reference method, there is a modification in the original value. In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument.

    What is call by reference in PHP?

    In case of PHP call by reference, actual value is modified if it is modified inside the function. In such case, you need to use & (ampersand) symbol with formal arguments. The & represents reference of the variable. Let's understand the concept of call by reference by the help of examples.

    What is call by value and call by reference explain with example?

    Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter. While Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter.