Let’s now look at the four different ways of creating strings.
Creating Strings Using Single quotes: The simplest way to create a string is to use single quotes.
Variable names can be used inside double quotes and their values will be displayed. In addition to variable interpolations, the double quote string can also escape more special characters such as “\n for a linefeed, \$ dollar for the dollar sign” etc.
More examples Let’s suppose that we have the following code This heredoc methodology is used to create fairly complex strings as compared to double quotes.
If you don’t do it yourself, on a debug build, the engine will shout at you about a memory leak you just created. The “releasing” is a simple operation : decrement the reference counter of the string, if it falls to zero, the API will free the string for you.
If you pass 1, you ask for what we called a “persistent” allocation, that is the engine will use a traditional C . If you forget to release a string, you will very likely create a memory leak. That is what we do : we change the final ‘r’ in ‘bar’ with a ‘z’, for ‘baz’.
It adds memory management facilities, so that a same string can be shared in several places without the need to duplicate it.
Also, some strings are “interned”, that is they are “persistent” allocated and specially managed by the memory manager so that they don’t get destroyed across several requests.
And then we display it, and free memory of every string.
Note that we forgot the hash value (if it were computed before, no need to think about that detail). Like we already said, the hash is used if the is used as part of Hash Tables.