file_put_contents

  • PHP has a convenience function called file_put_contents that automatically opens a file for writing, writes the contents of a string into it, and then closes the file.

    We can accomplish the same task in Ruby by opening a file for writing and passing a block that writes the contents. The file will be automatically closed when the block exits.

    PHP

    $contents = 'hello';
    file_put_contents('/path/to/foobar', $contents);

    Ruby

    contents = 'hello'
    File.open('/path/to/foobar', 'w') do |f|
      f.write contents
    end

    see also

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.