Using PHP strings as element ID in HTML
I’m trying to dynamically create <p>
elementss with different ids to "fill" them with JavaScript’s innerHTML
property.
Firstly I try to create 50 times:
echo "<p id=space1></p>";
But the number up going like space2, space3 etc.
I made this loop to generate the incrementing numbers and put it together with the string, and echo it:
for ($x = 1; $x <= 50; $x++) {
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
}
But when I run it and check the HTML code on the page, all I see is:
<p id=""></p>
But when I check the arrays by directly echoing them, the names get displayed correctly.
So how can I use my php strings as element ID in HTML?
php html arrays
add a comment |
I’m trying to dynamically create <p>
elementss with different ids to "fill" them with JavaScript’s innerHTML
property.
Firstly I try to create 50 times:
echo "<p id=space1></p>";
But the number up going like space2, space3 etc.
I made this loop to generate the incrementing numbers and put it together with the string, and echo it:
for ($x = 1; $x <= 50; $x++) {
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
}
But when I run it and check the HTML code on the page, all I see is:
<p id=""></p>
But when I check the arrays by directly echoing them, the names get displayed correctly.
So how can I use my php strings as element ID in HTML?
php html arrays
add a comment |
I’m trying to dynamically create <p>
elementss with different ids to "fill" them with JavaScript’s innerHTML
property.
Firstly I try to create 50 times:
echo "<p id=space1></p>";
But the number up going like space2, space3 etc.
I made this loop to generate the incrementing numbers and put it together with the string, and echo it:
for ($x = 1; $x <= 50; $x++) {
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
}
But when I run it and check the HTML code on the page, all I see is:
<p id=""></p>
But when I check the arrays by directly echoing them, the names get displayed correctly.
So how can I use my php strings as element ID in HTML?
php html arrays
I’m trying to dynamically create <p>
elementss with different ids to "fill" them with JavaScript’s innerHTML
property.
Firstly I try to create 50 times:
echo "<p id=space1></p>";
But the number up going like space2, space3 etc.
I made this loop to generate the incrementing numbers and put it together with the string, and echo it:
for ($x = 1; $x <= 50; $x++) {
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
}
But when I run it and check the HTML code on the page, all I see is:
<p id=""></p>
But when I check the arrays by directly echoing them, the names get displayed correctly.
So how can I use my php strings as element ID in HTML?
php html arrays
php html arrays
edited Jan 2 at 1:19


Funk Forty Niner
1
1
asked Jan 1 at 23:55


SnoenySnoeny
92
92
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You have a typo in retrieving your array.
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
Your first line it's called $xarray and your second line it's called $xarrray (extra r)
$xarray=array();
for ($x = 1; $x <= 50; $x++) {
$xarray[$x] = "space" . $x;
echo '<p id="{$xarray[$x]}"></p>';
}
Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.
– Snoeny
Jan 2 at 0:36
It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes
– Second2None
Jan 2 at 0:38
1
@Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc
– ivanivan
Jan 2 at 0:42
add a comment |
<p id="<?php echo $php_variable; ?>"></p>
This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.
add a comment |
Try this:
$xarray=array();
for ($x = 1; $x <= 50; $x++)
{
$xarray[$x] = "space" . $x;
echo "<p id='" . $xarray[$x] . "'></p>";
}
tried it and still just get this as html code: <p id=""></p>
– Snoeny
Jan 2 at 0:29
@Snoeny there was a typo, sorry, try again now :)
– Ass3mbler
Jan 2 at 0:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999864%2fusing-php-strings-as-element-id-in-html%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have a typo in retrieving your array.
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
Your first line it's called $xarray and your second line it's called $xarrray (extra r)
$xarray=array();
for ($x = 1; $x <= 50; $x++) {
$xarray[$x] = "space" . $x;
echo '<p id="{$xarray[$x]}"></p>';
}
Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.
– Snoeny
Jan 2 at 0:36
It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes
– Second2None
Jan 2 at 0:38
1
@Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc
– ivanivan
Jan 2 at 0:42
add a comment |
You have a typo in retrieving your array.
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
Your first line it's called $xarray and your second line it's called $xarrray (extra r)
$xarray=array();
for ($x = 1; $x <= 50; $x++) {
$xarray[$x] = "space" . $x;
echo '<p id="{$xarray[$x]}"></p>';
}
Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.
– Snoeny
Jan 2 at 0:36
It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes
– Second2None
Jan 2 at 0:38
1
@Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc
– ivanivan
Jan 2 at 0:42
add a comment |
You have a typo in retrieving your array.
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
Your first line it's called $xarray and your second line it's called $xarrray (extra r)
$xarray=array();
for ($x = 1; $x <= 50; $x++) {
$xarray[$x] = "space" . $x;
echo '<p id="{$xarray[$x]}"></p>';
}
You have a typo in retrieving your array.
$xarray = array($x => "space" . $x);
echo "<p id=$xarrray[$x]></p>";
Your first line it's called $xarray and your second line it's called $xarrray (extra r)
$xarray=array();
for ($x = 1; $x <= 50; $x++) {
$xarray[$x] = "space" . $x;
echo '<p id="{$xarray[$x]}"></p>';
}
answered Jan 2 at 0:30


Second2NoneSecond2None
1,1281416
1,1281416
Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.
– Snoeny
Jan 2 at 0:36
It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes
– Second2None
Jan 2 at 0:38
1
@Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc
– ivanivan
Jan 2 at 0:42
add a comment |
Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.
– Snoeny
Jan 2 at 0:36
It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes
– Second2None
Jan 2 at 0:38
1
@Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc
– ivanivan
Jan 2 at 0:42
Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.
– Snoeny
Jan 2 at 0:36
Thanks, I totally oversaw that :/ After removing this typo it works exactly the way i tried it in the question.
– Snoeny
Jan 2 at 0:36
It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes
– Second2None
Jan 2 at 0:38
It happens :) If you plan on using the array you should initiate it outside the loop to stop anything unexpected from happening. Also don't forget to wrap your p id in either single or double quotes
– Second2None
Jan 2 at 0:38
1
1
@Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc
– ivanivan
Jan 2 at 0:42
@Snoeny you may want to look at using an IDE - many will warn you about undefined variables, etc. even though PHP is very loose with declaration vs use much less data type etc
– ivanivan
Jan 2 at 0:42
add a comment |
<p id="<?php echo $php_variable; ?>"></p>
This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.
add a comment |
<p id="<?php echo $php_variable; ?>"></p>
This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.
add a comment |
<p id="<?php echo $php_variable; ?>"></p>
This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.
<p id="<?php echo $php_variable; ?>"></p>
This is because the server runs and compile before the browser renders the view/template. On rendering the above P element in your DOM, $php_variable would have been resolved and echoed into the quotes.
answered Jan 1 at 23:59


JoshuaJoshua
1025
1025
add a comment |
add a comment |
Try this:
$xarray=array();
for ($x = 1; $x <= 50; $x++)
{
$xarray[$x] = "space" . $x;
echo "<p id='" . $xarray[$x] . "'></p>";
}
tried it and still just get this as html code: <p id=""></p>
– Snoeny
Jan 2 at 0:29
@Snoeny there was a typo, sorry, try again now :)
– Ass3mbler
Jan 2 at 0:35
add a comment |
Try this:
$xarray=array();
for ($x = 1; $x <= 50; $x++)
{
$xarray[$x] = "space" . $x;
echo "<p id='" . $xarray[$x] . "'></p>";
}
tried it and still just get this as html code: <p id=""></p>
– Snoeny
Jan 2 at 0:29
@Snoeny there was a typo, sorry, try again now :)
– Ass3mbler
Jan 2 at 0:35
add a comment |
Try this:
$xarray=array();
for ($x = 1; $x <= 50; $x++)
{
$xarray[$x] = "space" . $x;
echo "<p id='" . $xarray[$x] . "'></p>";
}
Try this:
$xarray=array();
for ($x = 1; $x <= 50; $x++)
{
$xarray[$x] = "space" . $x;
echo "<p id='" . $xarray[$x] . "'></p>";
}
edited Jan 2 at 0:34
answered Jan 2 at 0:01
Ass3mblerAss3mbler
3,11611617
3,11611617
tried it and still just get this as html code: <p id=""></p>
– Snoeny
Jan 2 at 0:29
@Snoeny there was a typo, sorry, try again now :)
– Ass3mbler
Jan 2 at 0:35
add a comment |
tried it and still just get this as html code: <p id=""></p>
– Snoeny
Jan 2 at 0:29
@Snoeny there was a typo, sorry, try again now :)
– Ass3mbler
Jan 2 at 0:35
tried it and still just get this as html code: <p id=""></p>
– Snoeny
Jan 2 at 0:29
tried it and still just get this as html code: <p id=""></p>
– Snoeny
Jan 2 at 0:29
@Snoeny there was a typo, sorry, try again now :)
– Ass3mbler
Jan 2 at 0:35
@Snoeny there was a typo, sorry, try again now :)
– Ass3mbler
Jan 2 at 0:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999864%2fusing-php-strings-as-element-id-in-html%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown