PHP and WDDX - Page 3
September 4, 2002
An optional second parameter to wddx_serialize_value() lets you add
a human- readable comment to the resulting packet. Listing 5.7 is a variant of
Listing 5.5 that demonstrates this, with the output shown in Listing 5.8.
Going to the Source
If you're using your web browser to view the examples in the section,
you may wonder why the output you see on your screen doesn't match the
output shown here. Well, that's because web browsers tend to hide tags
that they don't know about; consequently, the WDDX tags generated during
the serialization process are not displayed in the rendered web page.
The solution is fairly simple: use the browser's View Source command to
view the raw source for the web page, and you should see the full uncensored
output.
Listing 5.7 Adding a Comment to a WDDX Packet
<?php
$flavors = array("strawberry", "chocolate", "raspberry", "peach");
print wddx_serialize_value($flavors, "A WDDX representation of my favorite
icecream flavors");
?>
Listing 5.8 A WDDX Packet with a Human-Readable Comment in the
Header
<wddxPacket version='1.0'>
<header>
<comment>
A WDDX representation of my favorite icecream flavors
</comment>
</header>
<data>
<array length='4'>
<string>strawberry</string>
<string>chocolate</string>
<string>raspberry</string>
<string>peach</string>
</array>
</data>
</wddxPacket>
The wddx_serialize_vars() Function
The wddx_serialize_value() function cannot accept more than a single
variable. However, it's also possible to serialize more than one variable
at a time with the wddx_serialize_vars() function, which can accept
multiple variables for serialization as function arguments. Listing 5.9
demonstrates how this works.
Listing 5.9 Serializing Multiple Values with wddx_serialize_vars()
<?php
$phrase = "The game's afoot";
$animals = array("parrot" => "Polly", "hippo" => "Hal", "dog" => "Rover",
"squirrel" => "Sparky");
print wddx_serialize_vars("phrase", "animals");
?>
Note that wddx_serialize_vars() requires the names of the variables
to be serialized as string arguments.
Listing 5.10 displays the result of a wddx_serialize_vars() run.
Listing 5.10 A WDDX Packet Generated via
wddx_serialize_vars()
<wddxPacket version='1.0'>
<header/>
<data>
<struct>
<var name='phrase'>
<string>The game's afoot</string>
</var>
<var name='animals'>
<struct>
<var name='parrot'>
<string>Polly</string>
</var>
<var name='hippo'>
<string>Hal</string>
</var>
<var name='dog'>
<string>Rover</string>
</var>
<var name='squirrel'>
<string>Sparky</string>
</var>
</struct>
</var>
</struct>
</data>
</wddxPacket>
PHP and WDDX - Page 2
XML and PHP
PHP and WDDX - Page 4
|