-
Notifications
You must be signed in to change notification settings - Fork 0
/
testando_variaveis.php
209 lines (142 loc) · 4.94 KB
/
testando_variaveis.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Testando variáveis</title>
</head>
<body>
<div class="container">
<h3>TESTANDO VARIÁVEIS</h3>
</div>
<div class="container">
<h3>VERIFICAR O TIPO DE VARIÁVEL</h3>
<p>
O PHP contém um conjunto vasto de funções que permitem avaliar que tipo de dados estão guardados dentro de uma variável.
</p>
<?php
$name = 'My name';
$age = 30;
$awake = true;
if(is_array($name)){
echo 'É um array.';
} else {
echo 'Não é um array.';
echo '<pre>';
print_r(gettype($name));
echo '</pre>';
}
if(is_bool($awake)){
echo 'É um valor booleano.'.'<br/> <br/>';
}
?>
<p>
Outras funções similares são: <br>
is_callable()<br>
is_float() ou is_double()<br>
is_real()<br>
is_int() ou is_integer()<br>
is_long()<br>
is_null()<br>
is_numeric()<br>
is_object()<br>
is_string()<br>
<br>
</p>
</div>
<div class="container">
<p>
Acontece inúmeras vezes no nosso código a necessidade de verificar ou testar a existência de variáveis ou do seu valor.
Para isso o PHP contém em conjunto de construções internas que nos permitem fazer essa verificação.
</p>
<p>
"ISSET" - esta construção permite verificar se uma variavel está ou não definida. Ela retorna verdadeiro se a variável existe, e falso se não existe.
</p>
</div>
<div class="container">
<?php
$a = 1;
if(isset($a)){
echo "A variavel existe! <br>";
} else {
echo 'A variável Não existe! <br>';
}
?>
</div>
<div class="container">
<p>
Se tivermos uma variavel definida, mas o seu valor for null, é considerada como uma variável inexistente.
</p>
<?php
$b = null;
echo isset($b) ? 'sim' : 'não';
?>
</div>
<div class="container">
<h3>EMPTY</h3>
<p>
Verifica se a variável tem um valor vazio (null, 0, false ou strong vazia) e retorna verdadeiro ou falso.
</p>
<?php
$x = 'João';
empty($b); # false
$y = false;
empty($y); # true
$nomes = [];
empty($nomes); # true
$outro = null;
empty($outro); #true
?>
</div>
<div class="container">
<h3>IS_NULL</h3>
<p>
Verifica se a variável tem um valor null ou não;
</p>
<?php
$x = 'João';
is_null($b); # false
$y = null;
is_null($y); # true
?>
</div>
<div class="container">
<h3>UNSET</h3>
<p>
Desvilcula o valor a variável. tornando-a nula.
</p>
<?php
$a = 'João';
unset($a);
$b = 'Antonio';
$b = null;
?>
</div>
<div class="container">
<h3>NULL COALESCING OPERATOR</h3>
<p>
Coalescência significa aglutinação ou junção de itens separados.
Basicamente este operador é um atalho para alguns casos do operador condicional ternário.
</p>
<?php
$x = null;
$nome = $x ?? 'Sem nome!';
// Este exemplo é semelhante a:
$nome1 = isset($x) ? $x : 'Sem nome';
// Portanto, se x for null, então é atribuido o valor definido à frente dos dois sinais de interrogação.
$sobrenome = null;
$sobrenome ??= 'Apelido desconhecido.';
// Esta forma é ainda mais simples de se utilizar o Operador de coalescência.
// neste caso apelido = 'Apelido desconhecido'
echo $nome;
echo '<br>';
echo $sobrenome;
?>
<p>
</p>
</body>
</html>