-
Notifications
You must be signed in to change notification settings - Fork 1
/
php.php
104 lines (89 loc) · 2.92 KB
/
php.php
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Basic syntax
<?php
// PHP Code
?>
//A PHP file normally contains HTML tags, and some PHP scripting code
//Declaring variables
<?php
$txt = "Hello world!"; //A variable starts with the $ sign, followed by the name of the variable
$x = 5;
$y = 10.5;
?> //Variable names are case-sensitive ($age and $AGE are two different variables)
//Output variables
<?php
$php = "PHP cheatsheet.";
echo "I am learning with a $php!";
?>
//The PHP echo statement is often used to output data to the screen
//Output the sum of two variables
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
//Data Types
/*
* PHP supports the following data types:
* 1. String
* 2. Integer
* 3. Float
* 4. Boolean
* 5. Array
* 6. Object
* 7. NULL
* 8. Resource
*/
<?php
$x = "Hello world"; //STRING
$x = 1000; //INTEGER
$x = 1.11; //FLOAT
$x = true; //or $x = false; //BOOLEAN
$x = array("one", "two", "three"); //ARRAY
?>
//Constants
<?php
define("color", "red"; true);
echo color;
?>
//Built-in functions
<?php
// For strings
$var = "This is a string";
strtolower($var); // Converts the string to lower case
strtoupper($var); // Converts the string to upper case
strlen($var); // Tells us how many characters there are in the string
$var[0]; // Indexing the characters in the string
$var[0] = "L"; //Replaces the indexed value in the string
str_replace("string", "phrase", $var); // Replaces a specific word in a string
substr($var, 8, /*optional, you can tell the number of the last character you want to index*/); // Isolates the string from index 8 and creates a subset with it
// For numbers
$num = 100;
abs($num); // Gives you the absolute value of a number
pow($num, $num); // Gives you the exponencial number
sqrt($num); // Gives you the square root of a number
max($num, $num); // Compares two numbers and tells you the bigger one
min($num, $num); // Compares two numbers and tells you the smaller one
round($num); // Rounds the number to its closest integer
ceil($num); // Rounds the number to its upper integer
floor($num); // Rounds the number to its lower integer
?>
// Operations with numbers
<?php
$number = 10;
// Common operations
$number++; // Increases the variable $number by a number
$number--; // Decreases the variable $number by a number
$number += 10; // This is the equivalent to $number = $number + 10, so the output should be 20
$number -= 10; // This is the equivalent to $number = $number - 10, so the output should be 0
$number *= 10; // This is the equivalent to $number = $number * 10, so the output should be 100
$number /= 10; // This is the equivalent to $number = $number / 10, so the output should be 1
?>
// Getting user input
<?php
<form action="/*the name of the php page you're working on*/" method="get">
Input: <input type="text" name="input" placeholder="Your input" />
<input type="submit" />
</form>
<br>
<?php echo $_GET["input"] ?>
?>