返回介绍

maybe_serialize()

发布于 2017-09-11 01:48:11 字数 3838 浏览 1037 评论 0 收藏 0

maybe_serialize( string|array|object $data )

Serialize data, if needed.


description


参数

$data

(string|array|object) (Required) Data that might be serialized.


返回值

(mixed) A scalar data


源代码

File: wp-includes/functions.php

function maybe_serialize( $data ) {
	if ( is_array( $data ) || is_object( $data ) )
		return serialize( $data );

	// Double serialization is required for backward compatibility.
	// See https://core.trac.wordpress.org/ticket/12930
	// Also the world will end. See WP 3.6.1.
	if ( is_serialized( $data, false ) )
		return serialize( $data );

	return $data;
}

更新日志

Versiondescription
2.0.5Introduced.

相关函数

Uses

  • wp-includes/functions.php: is_serialized()

Used By

  • wp-includes/option.php: update_network_option()
  • wp-includes/option.php: add_network_option()
  • wp-includes/deprecated.php: update_usermeta()
  • wp-includes/option.php: update_option()
  • wp-includes/option.php: add_option()
  • wp-includes/meta.php: delete_metadata()
  • wp-includes/meta.php: update_metadata_by_mid()
  • wp-includes/meta.php: add_metadata()
  • wp-includes/meta.php: update_metadata()
  • Show 4 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Examples

    ‘Hello World!’, ‘foo’ => ‘bar’ );
    echo maybe_serialize( $data );
    // a:2:{i:1;s:12:”Hello World!”;s:3:”foo”;s:3:”bar”;}

    // A serialized string will be serialized again.
    $data = ‘a:2:{i:1;s:12:”Hello World!”;s:3:”foo”;s:3:”bar”;}’;
    echo maybe_serialize( $data );
    // s:50:”a:2:{i:1;s:12:”Hello World!”;s:3:”foo”;s:3:”bar”;}”;

    ?>

  2. Basic Examples

    
    <?php
    
    // Strings are returned untouched.
    $data = 'Hello World!';
    echo maybe_serialize( $data );
    // Hello World!
    
    // Integers, floats and boolean values are also returned untouched.
    $data = 55;
    echo maybe_serialize( $data );
    // 55
    
    $data = 4.560
    echo maybe_serialize( $data );
    // 4.560
    
    $data = true;
    $data = maybe_serialize( $data );
    // $data = true;
    
    $data = null;
    $data = maybe_serialize( $data );
    // $data = null
    
    // An array or object will be returned as a serialized string.
    $data = array( 1 => 'Hello World!', 'foo' => 'bar' );
    echo maybe_serialize( $data );
    // a:2:{i:1;s:12:"Hello World!";s:3:"foo";s:3:"bar";}
    
    // A serialized string will be serialized again.
    $data = 'a:2:{i:1;s:12:"Hello World!";s:3:"foo";s:3:"bar";}';
    echo maybe_serialize( $data );
    // s:50:"a:2:{i:1;s:12:"Hello World!";s:3:"foo";s:3:"bar";}";
    
    ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文