Tim
Tim

Reputation: 2592

How can I identify what part of my code caused an element on my page?

My website looks like the following:

Hello World

And that is caused by the PHP code in index.php

<?php

$var = 'Hello ';
$Var = 'World';
echo "$var $Var";

?>

So, looking at my website and seeing that it says "Hello World", is there any way to analyse it (e.g. with web inspector) to find the code line that creates the text?

I am editing some code written by someone else, and I want to remove an element, but I don't know what bit of code is causing it - and I can't just search through the file with Ctrl+F because it is a variable element, responding to previous actions.

Upvotes: 0

Views: 105

Answers (5)

Tim
Tim

Reputation: 2592

First I looked through to find the general section (Alex's answer).

Then I ended up commenting out a large section of the page that looked likely until the element I wanted to go disappeared (along with others) then gradually shrinking the comment until it removed that part.

Upvotes: 0

Jack
Jack

Reputation: 302

Maybe

<?php $var = 'Hello <!--'.__LINE__.'-->'; $Var = 'World <!--'.__LINE__.'-->'; echo "$var $Var <!--'.__LINE__.'-->'; ?>

is kindof what you're looking for ..

Upvotes: 1

i alarmed alien
i alarmed alien

Reputation: 9520

Make a copy of the file you're looking at, and then add a unique string to each piece of code that generates output for the page.

For example:

<?php

$var = 'Hello ';
$Var = 'World';

// add in identifying info:
$var = '<p>Start of $var</p>' . $var . '<p>End of $var</p>';
$Var = '<p>Start of $Var</p>' . $var . '<p>End of $Var</p>';

echo "$var $Var";

If you have functions returning output, you can also wrap them with "Start of..." and "End of..." text to work out what html they are producing.

This is a very easy way to track down what code is producing what output. Obviously it is not suitable for a production site--you can use html comments if you are trying to do this on a production site, but I would hope that you have a development environment for this kind of work!

Upvotes: 1

Mauricio Ribeiro
Mauricio Ribeiro

Reputation: 86

"is there any way to analyse it (e.g. with web inspector) to see that the place that creates the text is line 5?" No.

PHP is server side, and when your browser load your page, the PHP code comes as HTML (client side)

Upvotes: 1

Alex
Alex

Reputation: 4774

You can't see the PHP code in the browser, so it won't help you much.

You should go through the code and track that piece of code instead of searching the HTML.

Upvotes: 1

Related Questions