2014. szeptember 11., csütörtök

PHP do...while ciklus

Hátultesztelő ciklus, addig fut, amíg igaz a feltétel. Egyszer mindenképp lefut!

pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.7 The do...while Statement</title>
</head>
<body>
<div>
<?php
$num = 221;
do {
    print "Execution number: $num<br />\n";
    $num++;
} while ( $num > 200 && $num < 400 );
?>
</div>
</body>
</html>


Eredmény:

Execution number: 221
Execution number: 222
Execution number: 223
Execution number: 224
Execution number: 225
Execution number: 226
Execution number: 227
Execution number: 228
Execution number: 229
Execution number: 230
Execution number: 231
Execution number: 232
Execution number: 233
Execution number: 234
Execution number: 235
Execution number: 236
Execution number: 237
Execution number: 238
Execution number: 239
Execution number: 240
Execution number: 241
Execution number: 242
Execution number: 243
Execution number: 244
Execution number: 245
Execution number: 246
Execution number: 247
Execution number: 248
Execution number: 249
Execution number: 250
Execution number: 251
Execution number: 252
Execution number: 253
Execution number: 254
Execution number: 255
Execution number: 256
Execution number: 257
Execution number: 258
Execution number: 259
Execution number: 260
Execution number: 261
Execution number: 262
Execution number: 263
Execution number: 264
Execution number: 265
Execution number: 266
Execution number: 267
Execution number: 268
Execution number: 269
Execution number: 270
Execution number: 271
Execution number: 272
Execution number: 273
Execution number: 274
Execution number: 275
Execution number: 276
Execution number: 277
Execution number: 278
Execution number: 279
Execution number: 280
Execution number: 281
Execution number: 282
Execution number: 283
Execution number: 284
Execution number: 285
Execution number: 286
Execution number: 287
Execution number: 288
Execution number: 289
Execution number: 290
Execution number: 291
Execution number: 292
Execution number: 293
Execution number: 294
Execution number: 295
Execution number: 296
Execution number: 297
Execution number: 298
Execution number: 299
Execution number: 300
Execution number: 301
Execution number: 302
Execution number: 303
Execution number: 304
Execution number: 305
Execution number: 306
Execution number: 307
Execution number: 308
Execution number: 309
Execution number: 310
Execution number: 311
Execution number: 312
Execution number: 313
Execution number: 314
Execution number: 315
Execution number: 316
Execution number: 317
Execution number: 318
Execution number: 319
Execution number: 320
Execution number: 321
Execution number: 322
Execution number: 323
Execution number: 324
Execution number: 325
Execution number: 326
Execution number: 327
Execution number: 328
Execution number: 329
Execution number: 330
Execution number: 331
Execution number: 332
Execution number: 333
Execution number: 334
Execution number: 335
Execution number: 336
Execution number: 337
Execution number: 338
Execution number: 339
Execution number: 340
Execution number: 341
Execution number: 342
Execution number: 343
Execution number: 344
Execution number: 345
Execution number: 346
Execution number: 347
Execution number: 348
Execution number: 349
Execution number: 350
Execution number: 351
Execution number: 352
Execution number: 353
Execution number: 354
Execution number: 355
Execution number: 356
Execution number: 357
Execution number: 358
Execution number: 359
Execution number: 360
Execution number: 361
Execution number: 362
Execution number: 363
Execution number: 364
Execution number: 365
Execution number: 366
Execution number: 367
Execution number: 368
Execution number: 369
Execution number: 370
Execution number: 371
Execution number: 372
Execution number: 373
Execution number: 374
Execution number: 375
Execution number: 376
Execution number: 377
Execution number: 378
Execution number: 379
Execution number: 380
Execution number: 381
Execution number: 382
Execution number: 383
Execution number: 384
Execution number: 385
Execution number: 386
Execution number: 387
Execution number: 388
Execution number: 389
Execution number: 390
Execution number: 391
Execution number: 392
Execution number: 393
Execution number: 394
Execution number: 395
Execution number: 396
Execution number: 397
Execution number: 398
Execution number: 399

PHP while ciklus

Elöltesztelő ciklus, addig fut, amíg a feltétel igaz.

pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.6 A while Statement</title>
</head>
<body>
<div>
<?php
$counter = 1;
while ( $counter <= 12 ) {
    print "$counter times 2 is ".($counter*2)."<br />";
    $counter++;
}
?>
</div>
</body>
</html>


Eredmény:
We are sorry that we have not met your expectations

feltétel ?: - "if függyvény"

Régebbi programnyelvekben és SQL-ben if függvényként ismeretes. Itt ternális (háromoperandusú) műveletjelnek hívjuk. lényege, hogy a feltételtől függően az igaz vagy hamis értéke szerint állítja be a visszaadott értéket.

pl:
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.5 Using the ? Operator</title>
</head>
<body>
<div>
<?php
$satisfied = "no";

$pleased = "We are pleased that you are happy with our service";
$sorry = "We are sorry that we have not met your expectations";

$text = ( $satisfied=="very" )?$pleased:$sorry;
print "$text";
?>
</div>
</body>
</html>

Eredmény:
We are sorry that we have not met your expectations

Több ágú elágazás 1 értéktől függően: switch

A vizsgált érték csak egyszerű típusú lehet. (Objektum, tömb... nem.)
Alakja:

switch ( kifejezés )
{
case érték1:
<utasítás>
break;
case érték2:
<utasítás>
break;
default:
 <utasítás>
break; //ez elvileg nem kötelező, de biztonságosabb, ha van
}

pl:

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.4 A switch Statement</title>
</head>
<body>
<div>
<?php
$satisfied = "no";
switch ( $satisfied ) {
    case "very":
        print "We are pleased that you are happy with our service";
        break;
    case "no":
        print "We are sorry that we have not met your expectations";
        break;
    default:
        print "Please take a moment to rate our service";
}
?>
</div>
</body>
</html>

Eredmény:
We are sorry that we have not met your expectations

2-nél több ágú elágazás: if, elseif...elseif, else


A szerkezet a következő:
if ( feltétel ) {
    } else if ( feltétel ) {
   } else {
    }

pl.

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.3 An if statement That Uses else and else if</title>
</head>
<body>
<div>
<?php
$satisfied = "no";
if ( $satisfied == "very" ) {
    print "We are pleased that you are happy with our service";
    // register customer satisfaction in some way
} else if ( $satisfied == "no" ) {
    print "We are sorry that we have not met your expectations";
    // request further feedback
} else {
    print "Please take a moment to rate our service";
    // present pulldown
}
?>
</div>
</body>
</html>


Eredmény:
We are sorry that we have not met your expectations

2014. szeptember 10., szerda

2 ágú elágazás - if ,,, else

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.2 An if Statement That Uses else</title>
</head>
<body>
<div>
<?php
// $satisfied = "very";
if ( $satisfied == "very" ) {
    print "We are pleased that you are happy with our service";
    // register customer satisfaction in some way
} else {
    print "Please take a moment to rate our service";
    // present pulldown
}
?>
</div>
</body>
</html>


Eredmény:
Notice: Undefined variable: satisfied in E:\STY PHP in 24 Hours\Hour 05\listing5.02.php on line 12 Please take a moment to rate our service

Egyszerű elágazás: if

Elágaztathatjuk a végrehajtást feltételtől függően.

<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 5.1 An if Statement</title>
</head>
<body>
<div>
<?php
$satisfied = "very";
if ( $satisfied == "very" ) {
    print "We are pleased that you are happy with our service";
    // register customer satisfaction in some way
}
?>
</div>
</body>
</html>


Eredmény:
We are pleased that you are happy with our service