.. _performances-joinfile: .. _joining-file(): Joining file() ++++++++++++++ Use `file() `_ to read lines separately. Applying ``join('', )`` or ``implode('', )`` to the `result `_ of `file() `_ provides the same results than using `file_get_contents() `_, but at a higher cost of memory and processing. If the delimiter is not ``''``, then ``implode()`` and ``file()`` are a better solution than ``file_get_contents()`` and ``str_replace()`` or ``nl2br()``. Always use ``file_get_contents()`` to get the content of a file as a string. Consider using `readfile() `_ to echo the content directly to the output. This analysis also checks for the reverse feature: loading a file with ``file_get_contents()`` and splitting it into rows with ``explode()`` or an alternative. Such association should be replaced by a single call to ``file()``, with may be the ``FILE_IGNORE_NEW_LINES``. .. code-block:: php See also `file_get_contents `_, `file `_ and `explode `_. Connex PHP features ------------------- + `csv `_ Suggestions ___________ * Use file_get_contents() instead of implode(file()) to read the whole file at once. * Use readfile() to echo the content to standard output ``stdout`` at once. * Use fopen() to read the lines one by one, generator style. Specs _____ +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Short name | Performances/JoinFile | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Rulesets | :ref:`All `, :ref:`Performances ` | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Exakat since | 0.8.4 | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | PHP Version | All | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Severity | Minor | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Time To Fix | Quick (30 mins) | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Precision | High | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Examples | :ref:`case-wordpress-performances-joinfile`, :ref:`case-spip-performances-joinfile` | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Available in | `Entreprise Edition `_, `Exakat Cloud `_ | +--------------+-------------------------------------------------------------------------------------------------------------------------+