I have been finding myself doing URLs like this:
$link = base_url('post') . '/' . $post_id . '/' . $slug . '/page/' . $page_num;
To form http://example.com/post/10/some-post-name/page/1
Needless to say, it’s pretty messy, but I can’t think of an alternative? Is there a better way write links with variables in it?
I am using Codeigniter as a framework if there is a solution involving it.
You have a few ways:
First, via sprintf:
Or via an array implode:
Or if you put all your values into variables, you can take advantage of string interpolation.
The last one is longer when you take into account the variable assignment block, but it combines succintness with readability.