Вы находитесь на странице: 1из 13

PHP y MySQL

UNACH - Coordinación de Tecnologías de Información

Unidad de servicios de Internet

LSC. Guillermo E. Betanzos Betanzos

betanzos@gmail.com
¿Qué es PHP?
 PHP:HyperText
Preprocessor

 OpenSource
Software

 FSF y OSI
Variables
 $a;

 $a_longish_variable_name;

 $2453;

 $sleepyZZZZ;
Tipos de datos
Type Example Description

Integer 5 A whole number

Double 3.234 A floating-point number

String "hello" A collection of characters

Boolean true One of the special values true or false

Object An instance of a class

Array An ordered set of keys and values


Ejemplo 1 – Probando el tipo de una
variable
1: <html>
2: <head>
3: <title>Ejemplo1. Probando el tipo de una variable </title>
4: </head>
5: <body>
6: <?php
7: $testing; // declare without assigning
8: print gettype( $testing ); // null
9: print "<br>";
10: $testing = 5;
11: print gettype( $testing ); // integer
12: print "<br>";
13: $testing = "five";
14: print gettype( $testing ); // string
15: print("<br>");
16: $testing = 5.0;
17: print gettype( $testing ); // double
18: print("<br>");
19: $testing = true;
20: print gettype( $testing ); // boolean
21: print "<br>";
22: ?>
23: </body>
24: </html>
Ejemplo 2 – Cambiando el tipo de dato
con settype()
1: <html>
2: <head>
3: <title> Cambiando el tipo de dato con settype() </title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: print gettype( $undecided ); // double
9: print " is $undecided<br>"; // 3.14
10: settype( $undecided, 'string' );
11: print gettype( $undecided ); // string
12: print " is $undecided<br>"; // 3.14
13: settype( $undecided, 'integer' );
14: print gettype( $undecided ); // integer
15: print " is $undecided<br>"; // 3
16: settype( $undecided, 'double' );
17: print gettype( $undecided ); // double
18: print " is $undecided<br>"; // 3.0
19: settype( $undecided, 'boolean' );
20: print gettype( $undecided ); // boolean
21: print " is $undecided<br>"; // 1
22: ?>
23: </body>
24: </html>
Ejemplo 3 – Cambiando el tipo de dato
con settype()
1: <html>
2: <head>
3: <title> Ejemplo 3 – Cambiando el tipo de dato con settype() </title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: $holder = ( double ) $undecided;
9: print gettype( $holder ) ; // double
10: print " is $holder<br>"; // 3.14
11: $holder = ( string ) $undecided;
12: print gettype( $holder ); // string
13: print " is $holder<br>"; // 3.14
14: $holder = ( integer ) $undecided;
15: print gettype( $holder ); // integer
16: print " is $holder<br>"; // 3
17: $holder = ( double ) $undecided;
18: print gettype( $holder ); // double
19: print " is $holder<br>"; // 3.14
20: $holder = ( boolean ) $undecided;
21: print gettype( $holder ); // boolean
22: print " is $holder<br>"; // 1
23: print "<hr>";
24: print "original variable type: ";
25: print gettype( $undecided ); // double
26: ?>
27: </body>
28: </html>
Operadores aritméticos

Operator Name Example Example Result


+ 10+3 13
Suma
- 10-3 7
Resta
/ 10/3 3.3333333333333
División
* 10*3 30
Multiplicación
% 10%3 1
Residuo
Operador de concatenación
 El operador de concatenación se representa por un
punto (.). Tratando ambos operandos como cadenas.

 El resultado de:
print “Hola"." mundo"

 Regresaría:
“Hola mundo"

 ¿Qué regresaría el siguiente ejemplo de código?


$centimetros = 212;
print “La longitud es ".($centimetros/100)." metros";
Operadores de asignación combinados

<?php
$x = 4;
$x = $x + 4; // $x ahora es igual a 8
?>
 También podría ser escrito como:
<?php
$x = 4;
$x += 4; // $x es igual a 8
?>
Operadores de asignación combinados

Operador Ejemplo Equivale a


+= $x += 5 $x = $x + 5
-= $x -= 5 $x = $x - 5
/= $x /= 5 $x = $x / 5
*= $x *= 5 $x = $x * 5
%= $x %= 5 $x = $x % 5
.= $x .= " test" $x = $x." test"
Incremento y decremento de variables
enteras
 $x = $x + 1; // $x $x se incrementa

 $x += 1; // $x se incrementa

 $x++; // $x se incrementa

 $x-; // $x se decrementa

 ++$x; // $x se decrementa

 -$x; // $x se decrementa
Operadores de comparación
Operator Name Ejemplo ($x es 4) Resultado

== Igualdad $x == 5 false

!= Desigual $x != 5 true

=== Idéntico $x === "7" false

> Mayor que $x > 4 false

>= Mayor igual que $x >= 4 true

< Menor que $x < 4 false

<= Menor igual que $x <= 4 true

Вам также может понравиться