maybe_serialize() - WordPress 函数参考中文文档

返回介绍

maybe_serialize()

发布于 2017-09-11 字数3840 浏览 902 评论 0

maybe_serialize( string|array|object $data )

Serialize data, if needed.


描述


参数

$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;
}

更新日志

Version 描述
2.0.5 Introduced.

相关函数

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";}";
    
    ?>
    

上一篇:maybe_redirect_404()

下一篇:maybe_unserialize()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!