# Constructing URLs [closed]

I'm a beginner in PHP.

Here my script

$url1 =$chemin.'include/aaa/url1.php?param='.$param.'&page='.$p;
$url2 =$chemin.'include/aaa/url1.php?param='.$param.'&page='.$total_pages;

//url2.php
$url3 =$chemin.'include/aaa/url2.php?page='.$p;$url4 =  $chemin.'include/aaa/url2.php?page='.$total_pages;

//url3.php
$url5 =$chemin.'include/aaa/url3.php?page='.$p;$url6 = $chemin.'include/aaa/url3.php?page='.$total_pages;


I have to write 10 more lines on the same way.

Is there a way to improve my script?

• Welcome to CodeReview@SE. Hard to tell: Please heed How do I ask a Good Question? regarding context. – greybeard Feb 9 at 23:54
• You seem to speak French, hence the $chemin variable. However, other variables in your code are written in English. Anyone who has to read your code must therefore be able to understand these two languages. Think about this before you continue. I write all my code in English only. And no, I am not a native English speaker, I just find it easier for working with other people to stick to one common language. – KIKO Software Feb 12 at 9:32 • Can we see how you are using the urls, as it seems unusual to store them as$url1, $url2, instead of in an array of$urls[]. If we can see how you are using them, we may be able to offer better suggestions. – bumperbox Feb 13 at 19:37

The way you currently doing it would require all the websites to be written manually. A possible alternative would be to implement recursion (loops).

The for loop:

For loops are a good way of repeating a section of code/similar code a number of times. The number of times to repeat this code can be known explicitly, or you can determine it through a simple calculation.

Syntax:

for($i = 0;$i < 10; $i++) { echo "I at at iteration: " .$i . "\r\n";
}


Meaning:

for(initialisation; condition; increment) {
Code to Execute
}


Implementation

As Hardy Mathew provided, a possible implementation could be: https://codereview.stackexchange.com/a/237250/218422

Other

Your urls look like php classes. If you are trying to include them with other classes, perhaps auto loading is what you're looking for: https://www.php.net/manual/en/language.oop5.autoload.php

you can simply use the for loop for such things,

$totalUrl = 16;// change this number as your requirement for($i=0;$<$totalUrl;$i++){$url = $chemin.'include/aaa/url.'$i'.php?param='.$param.'&page='.$p;
// and here your code -include this or do whatever you want
}

• Can you show how that assigns to $url1, $url2, etc? It's not clear how that occurs. – Toby Speight Feb 13 at 17:50
• I see a for loop problem and a concatenation problem. Furthermore, this is not a good Code Review ("you can do it like this" is not educational"). – mickmackusa Feb 14 at 3:28