Skip to content

Kévin Dunglas

Founder of Les-Tilleuls.coop (worker-owned cooperative). Creator of API Platform, FrankenPHP, Mercure.rocks, Vulcain.rocks and of some Symfony components.

Menu
  • Talks
  • Resume
  • Sponsor me
  • Contact
Menu

Casting PHP types to string

Posted on February 3, 2014May 31, 2022 by Kévin Dunglas

PHP

Casting PHP types is not easier as it seems. Type juggling in PHP is not obvious. In Java variables can always be represented as strings, without throwing errors. This is not the case with PHP and I’ve not found the detailed behavior of string casting in the official documentation. Here it is.
Scalar types (boolean, integer, float, and string), NULL and, oddly, resources can be cast to string without errors. Scalars are converted to their string representation (e.g. 12.3 will become "12.3"), NULL to an empty string and resources like "Resource id #2".
The going gets tough when it comes to arrays and objects. Arrays are converted to the string "Array" but a notice is thrown. Concerning the objects, no problem if they are instances of classes implementing the magic __toString() method. However, if the class, such as \stdClass or \DateTime,does not implement this method, a catchable fatal error is thrown.

Finally, a code snippet to test if a variable can be cast to the string type:

/**
 * Tests if a variable can be casted to string without error
 *
 * @param  mixed $var
 * @return boolean
 */
function canBeString($var)
{
    return !is_array($var) && (!is_object($var) || method_exists($var, '__toString'));
}

Related posts:

  1. Introducing PropertyInfo: a PHP component to find types and doc of properties
  2. Introducing the phpdoc-to-typehint Converter: add scalar type hints and return type declarations to your projects
  3. JSON Columns and Doctrine DBAL 3 Upgrade
  4. PHP 7: Introducing a domain name validator and making the URL validator stricter

Leave a ReplyCancel reply

Social

  • Bluesky
  • GitHub
  • LinkedIn
  • Mastodon
  • X
  • YouTube

Links

  • API Platform
  • FrankenPHP
  • Les-Tilleuls.coop
  • Mercure.rocks
  • Vulcain.rocks

Subscribe to this blog

Top Posts & Pages

  • JSON Columns and Doctrine DBAL 3 Upgrade
  • Securely Access Private Git Repositories and Composer Packages in Docker Builds
  • Preventing CORS Preflight Requests Using Content Negotiation
  • FrankenPHP: The Modern Php App Server, written in Go
  • Symfony's New Native Docker Support (Symfony World)
  • Develop Faster With FrankenPHP
  • How to debug Xdebug... or any other weird bug in PHP
  • HTTP compression in PHP (new Symfony AssetMapper feature)
  • PHP and Symfony Apps As Standalone Binaries
  • Generate a Symfony password hash from the command line

Tags

Apache API API Platform Buzz Caddy Docker Doctrine FrankenPHP Go Google GraphQL HTTP/2 Hydra hypermedia Hébergement Javascript JSON-LD Kubernetes La Coopérative des Tilleuls Les-Tilleuls.coop Lille Linux Mac Mercure Mercure.rocks Messagerie Instantanée MySQL performance PHP Punk Rock Python React REST Rock'n'Roll Schema.org Security SEO SEO Symfony Symfony Live Sécurité Ubuntu Web 2.0 webperf XML

Archives

Categories

  • DevOps (84)
    • Ubuntu (68)
  • Go (17)
  • JavaScript (46)
  • Mercure (7)
  • Opinions (91)
  • PHP (170)
    • API Platform (77)
    • FrankenPHP (9)
    • Laravel (1)
    • Symfony (97)
    • Wordpress (6)
  • Python (14)
  • Security (15)
  • SEO (25)
  • Talks (46)
© 2025 Kévin Dunglas | Powered by Minimalist Blog WordPress Theme