Skip to content

Kévin Dunglas

Founder of Les-Tilleuls.coop (worker-owned cooperative). Creator of API Platform, 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, 2014 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 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, ressources can be casted 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 ressources like "Resource id #2".
The going get tough when it comes to array 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 casted 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. PHP 7: Introducing a domain name validator and making the URL validator stricter
  4. Tag Suggestion for symfony

Leave a Reply Cancel reply

Follow me on Twitter

My Tweets

Subscribe to this blog

Recent Posts

  • New in Caddy 2.5: Redact Sensitive Data from Your Logs
  • Building Decentralized Web Apps with Solid and PHP
  • JSON Columns and Doctrine DBAL 3 Upgrade
  • Preventing CORS Preflight Requests Using Content Negotiation
  • Symfony’s New Native Docker Support (Symfony World)

Top Posts & Pages

  • Symfony UX Turbo: Do You Still Need JavaScript?! (SymfonyWorld)
  • Building Decentralized Web Apps with Solid and PHP
  • JSON Columns and Doctrine DBAL 3 Upgrade
  • PHP 7: Introducing a domain name validator and making the URL validator stricter
  • Using Next.js and Material UI Together
  • Say Hello to Mercure 0.10!
  • Preventing CORS Preflight Requests Using Content Negotiation
  • Generate a Symfony password hash from the command line
  • REST vs GraphQL: illustrated examples with the API Platform framework (PHPTour/SymfonyLive)
  • The Mercure.rocks Hub is now based on Caddy Web Server

Persistence in PHP with the Doctrine ORM

Persistence in PHP with the Doctrine ORM

Tags

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

Archives

Categories

  • DevOps (24)
  • Mercure (3)
  • Opinions (91)
  • Programming (178)
    • Android (5)
    • Go (11)
    • JavaScript (43)
    • PHP (136)
      • API Platform (60)
      • Symfony (89)
    • Python (14)
      • Django (5)
  • Security (15)
  • SEO (24)
  • Talks (37)
  • Ubuntu (68)
  • Wordpress (6)

Blogoliste

  • API Platform
  • Les-Tilleuls.coop
  • Mercure.rocks
  • Vulcain.rocks
© 2022 Kévin Dunglas | Powered by Minimalist Blog WordPress Theme