Lokang 

PHP and MySQL

Functions

Echo

echo is a function in PHP that outputs one or more strings to the screen. It is often used to print out the value of a variable or the result of a calculation.

Here is an example of how to use the echo function in PHP:

$name = "John";
echo "Hello, " . $name;  // Outputs "Hello, John"
$a = 5;
$b = 10;
echo $a + $b;  // Outputs "15"

You can also use the echo function to output multiple strings by separating them with a comma. For example:

echo "Hello, ", $name;  // Outputs "Hello, John"

Note that the echo function does not add a newline character at the end of the string, so if you want to output multiple strings on separate lines, you will need to use the newline character (\n) or the PHP_EOL constant. For example:

echo "Hello, ", $name, "\n";  // Outputs "Hello, John" on a separate line
echo "Hello, ", $name, PHP_EOL;  // Same as above

 

Print

print is a function in PHP that outputs a string to the screen. It is similar to the echo function, but print can only output a single string, whereas echo can output multiple strings.

Here is an example of how to use the print function in PHP:

$name = "John";
print "Hello, " . $name;  // Outputs "Hello, John"
$a = 5;
$b = 10;
print $a + $b;  // Outputs "15"

Like the echo function, the print function does not add a newline character at the end of the string. If you want to output multiple strings on separate lines, you will need to use the newline character (\n) or the PHP_EOL constant.

print "Hello, " . $name . "\n";  // Outputs "Hello, John" on a separate line
print "Hello, " . $name . PHP_EOL;  // Same as above

Note that print is a slower function than echo, so it is generally better to use echo when performance is important.

 

Print_r

print_r is a function in PHP that outputs the human-readable representation of a variable. It is often used for debugging purposes, as it allows you to see the contents of a variable, including arrays and objects.

Here is an example of how to use the print_r function in PHP:

$array = array(1, 2, 3, 4, 5);
print_r($array);  // Outputs "Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )"
$object = new stdClass();
$object->name = "John";
$object->age = 30;
print_r($object);  // Outputs "stdClass Object ( [name] => John [age] => 30 )"

You can also pass a second argument to print_r called return, which will cause the function to return the output as a string instead of printing it to the screen. This can be useful if you want to capture the output of print_r and use it in your code.

$output = print_r($array, true);
echo $output;  // Outputs "Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )"

 

Printf

printf is a function in PHP that outputs a formatted string to the screen. It is similar to the echo and print functions, but printf allows you to use placeholders in the string and pass in values for those placeholders as arguments.

Here is an example of how to use the printf function in PHP:

$name = "John";
$age = 30;
printf("Hello %s, you are %d years old.", $name, $age);  // Outputs "Hello John, you are 30 years old."

In the example above, %s is a placeholder for a string, and %d is a placeholder for an integer. You can use different placeholders depending on the type of data you want to output. For example:

printf("The value of pi is approximately %.2f.", pi());  // Outputs "The value of pi is approximately 3.14."

In the example above, %.2f is a placeholder for a floating-point number, and the .2 specifies that the number should be formatted with two decimal places.

You can also use the printf function to output multiple strings by separating them with a comma. For example:

printf("Hello, %s\n", $name);  // Outputs "Hello, John" on a separate line

Note that the printf function does not add a newline character at the end of the string, so if you want to output multiple strings on separate lines, you will need to use the newline character (\n) or the PHP_EOL constant.

 

var_dump is a function in PHP that outputs the type and value of a variable. It is often used for debugging purposes, as it provides more information about a variable than the print_r function.

Here is an example of how to use the var_dump function in PHP:

$array = array(1, 2, 3, 4, 5);
var_dump($array);  // Outputs "array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }"
$object = new stdClass();
$object->name = "John";
$object->age = 30;
var_dump($object);  // Outputs "object(stdClass)#1 (2) { ["name"]=> string(4) "John" ["age"]=> int(30) }"

As you can see, the var_dump function outputs the type of each value in the variable (e.g. "int" for integers, "string" for strings), as well as the actual value.

You can also pass multiple variables to the var_dump function, and it will output the type and value of each variable.

$a = 5;
$b = "hello";
var_dump($a, $b);  // Outputs "int(5) string(5) "hello""

Note that the var_dump function is generally more verbose than the print_r function, so it may produce a lot of output when used on large variables or data structures.