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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
|
<HTML
><HEAD
><TITLE
>Types</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.41"><LINK
REL="HOME"
TITLE="PHP3 Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Syntax and grammar"
HREF="lang-syntax.html"><LINK
REL="PREVIOUS"
TITLE="Syntax and grammar"
HREF="lang-syntax.html"><LINK
REL="NEXT"
TITLE="Variables"
HREF="variables.html"></HEAD
><BODY
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP3 Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="lang-syntax.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. Syntax and grammar</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="variables.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TYPES"
>Types</A
></H1
><P
> PHP supports the following types:
<P
></P
><UL
><LI
><P
><A
HREF="types.html#TYPES.INTEGER"
>integer</A
>
</P
></LI
><LI
><P
><A
HREF="types.html#TYPES.DOUBLE"
>floating-point numbers</A
>
</P
></LI
><LI
><P
><A
HREF="types.html#TYPES.STRING"
>string</A
>
</P
></LI
><LI
><P
><A
HREF="types.html#TYPES.ARRAY"
>array</A
>
</P
></LI
><LI
><P
><A
HREF="types.html#TYPES.OBJECT"
>object</A
>
</P
></LI
></UL
>
</P
><P
> The type of a variable is usually not set by the programmer;
rather, it is decided at runtime by PHP depending on the context
in which that variable is used.
</P
><P
> If you would like to force a variable to be converted to a certain
type, you may either <A
HREF="variables.html#LANG-SYNTAX.TYPECASTING"
>cast</A
> the variable or use
the <A
HREF="function.settype.html"
><B
CLASS="FUNCTION"
>settype()</B
></A
> function on it.
</P
><P
> Note that a variable may behave in different manners in certain
situations, depending on what type it is a the time. For more
information, see the section on <A
HREF="variables.html#VARIABLES.TYPE-JUGGLING"
>Type Juggling</A
>.
</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="TYPES.INTEGER"
>Integers</A
></H2
><P
> Integers can be specified using any of the following syntaxes:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><PRE
CLASS="PROGRAMLISTING"
>$a = 1234; # decimal number
$a = -123; # a negative number
$a = 0123; # octal number (equivalent to 83 decimal)
$a = 0x12; # hexadecimal number (equivalent to 18 decimal)</PRE
><P
></P
></DIV
>
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="TYPES.DOUBLE"
>Floating point numbers</A
></H2
><P
> Floating point numbers ("doubles") can be specified using any
of the following syntaxes:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><PRE
CLASS="PROGRAMLISTING"
>$a = 1.234;
$a = 1.2e3;</PRE
><P
></P
></DIV
>
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="TYPES.STRING"
>Strings</A
></H2
><P
> Strings can be specified using one of two sets of delimiters.
</P
><P
> If the string is enclosed in double-quotes ("), variables
within the string will be expanded (subject to some parsing
limitations). As in C and Perl, the backslash ("\") character
can be used in specifying special characters:
<DIV
CLASS="TABLE"
><P
><B
>Table 5-1. Escaped characters</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><TR
><TH
ALIGN="LEFT"
VALIGN="TOP"
>sequence</TH
><TH
ALIGN="LEFT"
VALIGN="TOP"
>meaning</TH
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="LITERAL"
>\n</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>newline</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="LITERAL"
>\r</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>carriage</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="LITERAL"
>\t</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>horizontal tab</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="LITERAL"
>\\</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>backslash</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="LITERAL"
>\$</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>dollar sign</TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
><TT
CLASS="LITERAL"
>\"</TT
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
>double-quote</TD
></TR
></TABLE
></DIV
>
</P
><P
> You can escape any other character, but a warning will be issued
at the highest warning level.
</P
><P
> The second way to delimit a string uses the single-quote ("'")
character, which does not do any variable expansion or backslash
processing (except for "\\" and "\'" so you can insert backslashes
and single-quotes in a singly-quoted string).
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="TYPES.ARRAY"
>Arrays</A
></H2
><P
> Arrays actually act like both hash tables (associative arrays)
and indexed arrays (vectors).
</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="TYPES.ARRAY.SINGLE-DIM"
>Single Dimension Arrays</A
></H3
><P
> PHP supports both scalar and associative arrays. In fact, there
is no difference between the two. You can create an array using
the <A
HREF="function.list.html"
><B
CLASS="FUNCTION"
>list()</B
></A
> or <A
HREF="function.array.html"
><B
CLASS="FUNCTION"
>array()</B
></A
>
functions, or you can explicitly set each array element value.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><PRE
CLASS="PROGRAMLISTING"
>
$a[0] = "abc";
$a[1] = "def";
$b["foo"] = 13;
</PRE
><P
></P
></DIV
>
</P
><P
> You can also create an array by simply adding values to
the array.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><PRE
CLASS="PROGRAMLISTING"
>
$a[] = "hello"; // $a[2] == "hello"
$a[] = "world"; // $a[3] == "world"
</PRE
><P
></P
></DIV
>
</P
><P
> Arrays may be sorted using the <A
HREF="function.asort.html"
><B
CLASS="FUNCTION"
>asort()</B
></A
>,
<A
HREF="function.arsort.html"
><B
CLASS="FUNCTION"
>arsort()</B
></A
>, <A
HREF="function.ksort.html"
><B
CLASS="FUNCTION"
>ksort()</B
></A
>,
<A
HREF="function.rsort.html"
><B
CLASS="FUNCTION"
>rsort()</B
></A
>, <A
HREF="function.sort.html"
><B
CLASS="FUNCTION"
>sort()</B
></A
>,
<A
HREF="function.uasort.html"
><B
CLASS="FUNCTION"
>uasort()</B
></A
>, <A
HREF="function.usort.html"
><B
CLASS="FUNCTION"
>usort()</B
></A
>, and
<A
HREF="function.uksort.html"
><B
CLASS="FUNCTION"
>uksort()</B
></A
> functions depending on the type of
sort you want.
</P
><P
> You can count the number of items in an array using the
<A
HREF="function.count.html"
><B
CLASS="FUNCTION"
>count()</B
></A
> function.
</P
><P
> You can traverse an array using <A
HREF="function.next.html"
><B
CLASS="FUNCTION"
>next()</B
></A
> and
<A
HREF="function.prev.html"
><B
CLASS="FUNCTION"
>prev()</B
></A
> functions. Another common way to
traverse an array is to use the <A
HREF="function.each.html"
><B
CLASS="FUNCTION"
>each()</B
></A
>
function.
</P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="TYPES.ARRAY.MULTI-DIM"
>Multi-Dimensional Arrays</A
></H3
><P
> Multi-dimensional arrays are actually pretty simple. For each
dimension of the array, you add another [key] value to the end:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><PRE
CLASS="PROGRAMLISTING"
>
$a[1] = $f; # one dimensional examples
$a["foo"] = $f;
$a[1][0] = $f; # two dimensional
$a["foo"][2] = $f; # (you can mix numeric and associative indices)
$a[3]["bar"] = $f; # (you can mix numeric and associative indices)
$a["foo"][4]["bar"][0] = $f; # four dimensional!
</PRE
><P
></P
></DIV
>
</P
><P
> You can "fill up" multi-dimensional arrays in many ways,
but the trickiest one to understand is how to use the
<A
HREF="function.array.html"
><B
CLASS="FUNCTION"
>array()</B
></A
> command for associative arrays.
These two snippets of code fill up the one-dimensional array in
the same way:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><PRE
CLASS="PROGRAMLISTING"
>
# Example 1:
$a["color"] = "red";
$a["taste"] = "sweet";
$a["shape"] = "round";
$a["name"] = "apple";
$a[3] = 4;
# Example 2:
$a = array(
"color" => "red",
"taste" => "sweet",
"shape" => "round",
"name" => "apple",
3 => 4
);
</PRE
><P
></P
></DIV
>
</P
><P
> The <A
HREF="function.array.html"
><B
CLASS="FUNCTION"
>array()</B
></A
> function can be nested for
multi-dimensional arrays:
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><PRE
CLASS="PROGRAMLISTING"
>
<?
$a = array(
"apple" => array(
"color" => "red",
"taste" => "sweet",
"shape" => "round"
),
"orange" => array(
"color" => "orange",
"taste" => "sweet",
"shape" => "round"
),
"banana" => array(
"color" => "yellow",
"taste" => "paste-y",
"shape" => "banana-shaped"
)
);
echo $a["apple"]["taste"]; # will output "sweet"
?>
</PRE
><P
></P
></DIV
>
</P
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="TYPES.OBJECT"
>Objects</A
></H2
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="TYPES.OBJECT.INIT"
>Object Initialization</A
></H3
><P
> To initialize an object, you use the new statement to instantiate
the object to a variable.
<DIV
CLASS="INFORMALEXAMPLE"
><P
></P
><PRE
CLASS="PROGRAMLISTING"
>class foo {
function do_foo () {
echo "Doing foo.";
}
}
$bar = new foo;
$bar -> do_foo ();
</PRE
><P
></P
></DIV
>
</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="lang-syntax.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="variables.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Syntax and grammar</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="lang-syntax.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Variables</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|