I am trying to play with php, I aint very good. But I figured its about time I started playing with it
What I have is say a page called ascr
Its filename is acsr.php
I have created a global php include, so I can play with different vars and include them dynamically.
So I have made a var called $shorttag = "acsr";
On my page I would like to use that shorttag to fire images and classes etc.
So for an image I want to display
img src="/img/<?php $shorttag; ?>.jpg"
and in other places perhaps,
div class="<?php $shorttag; ?>"
Is this the CORRECT way of doing stuff like this ?
//////////////////////////////////////
Example.
say I have folder with image in it called acsr.jpg
On my dynamic page I want to echo that image, but instead of using filename, I want to pull it in based on my php var , in this case its called $shorttag so essentially, $shorttag=acsr , and therefore I can spew out something like:
<?php $shorttag; ?>.jpg
( code above more than likely wrong )
But the result gives me ascr.jpg
You need to echo it so it is actually output.
div class="<?php echo $shorttag; ?>"
img src="/img/<?php echo $shorttag; ?>.jpg"
If PHP is configured to use short_open_tag (which is not usually recommended since it may be turned off on some servers)
img src="/img/<?= $shorttag ?>.jpg"
The rules change for PHP 5.4 and later, where you can use <?= ?> without having to turn on short_open_tag:
This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo. Use of this shortcut required short_open_tag to be on. Since PHP 5.4.0, <?= is always available.
Your examples is missing a way to echo out the value. You can either use echo like what Michael suggested, or use the syntax <?= $shorttag ?>.
Additionally, in your examples, you will be creating an image at /img/acsr.jpg and attaching a class called acsr to that div. If that is what you want, then yes, you're on the right track. Otherwise, you will need to explain more of what you wish to accomplish.
You're basically doing what I did with "pid" and "sid" (i.e., http://mysite.com/?pid=main_page&sid=sub_context) right?
You'll have to make sure your pages are protected from user tampering by ensuring your parameter values adhere to a standard (ex., regex replace all but a-zA-Z_- with ""). As for classes, you'd do best to avoid having them rely on a page id for styling. Make your styles global in nature.
Related
When using Laravel, you can use { $variable } instead of <?php echo $variable; ?>, can you do this when you are using PHP without Laravel?
Let me see if I got you right. You want to put variable elements inside your HTML template files without using the ugly "<?php echo ...; ?>" ?
What you need is a Template engine. You could get one of these ready-to-use template system and learn how to use them (but they don't have the exact behaviour you describe) or you could code your own parser.
I personnally made my own HTML parser, its behaviour is adapted to the use I make of it. For what you need, all you need is to read HTML files and replace using a regexp
^\{ (.+) \}$
with
$($1)
I have made 2 views: 'list' and 'maps'
The 'list' view has an exposed filter input field (called title)
I try made a link in the head-section of the list-view (text-format: PHP-code) to the maps-view.
My question is, how can I do this with the GET method?
This code don't work:
maps
That's easy :) you missed the echo:
maps
# ^^^^
edit: there is also a <?= opening tag (instead of <? and <?php) which automatically calls echo (e.g. <?= $_GET['title'] ?>) but some people find them bad. Especially that modern frameworks such as Symfony2 recommend to disable short_open_tags.
I am building my website completely in PHP. I am trying to make it as much flexible as possible.
I have seen there are some softwares made in PHP that are able to get a HTML page, and before showing it, the PHP code recognizes the code inside brackets {PHP Code} as PHP code, runs it and only then shows the final page.
<h1>Hi My Name is {echo $name}</h1>
How can I achieve the same? I know there is Smarty Code. But I do not want to learn Smarty, I just want to know how to check a HTML page with PHP, find every bracket and threat that as PHP before showing the page..?
Can you point me somewhere?
Are you looking for PHP's basic syntax?
If you enable short_open_tags (it usually is enabled by default), this will work:
<h1>Hi My Name is <?=$name?></h1>
otherwise, this will always work:
<h1>Hi My Name is <?php echo $name; ?></h1>
PHP is already a templating language - there often is no need to add another layer of templating on top of it.
I want to keep the template files separated from the php engine
In fact, you don't
Your template files would behave as native PHP files in every way.
So, there is asolutely no [logical] reason to prefer such a strange solution over native PHP.
use the php tags for the echo statement.
<h1>Hi my name is <?php echo $name; ?></h1>
Well, just point apache to index.php which includes phtml templates into itself. Use <?php ?> instead of { }.
I usually create modular websites, each part of the website being a .php file which will be included in the main pages.
Is it "better" to output HTML within PHP files using echo or to close each time the php tag ?> and open it each time I need to access a PHP function/variable.
V1:
<?php
$v1=$_POST['name'];
echo "Your name is".$v1;
echo $v1." if you want, you can log out";
?>
V2:
<?php $v1=$_POST['name']; ?>
Your name is <?php echo $v1; ?>
<?php echo $v1;?> if you want, you can log out
The thing is that between the php tags there's much more HTML code (echoed) than actual PHP.
Does it affect the script performance if I close the tags each time? And is it safe to acces variables declared in a previous block of php code?
EDIT1:
When closing the php tags isn't the server clearing some cache for that script, or something like that?
I think you can select whatever you want, but you should use it everywhere. For myself, second one is better
Definitely v2. Plus , you additionally should read this one : http://codeangel.org/articles/simple-php-template-engine.html (archive link: http://archive.is/CiHhD).
Using V2 would be better as it wouldn't break the syntax highlighting or code completion in many IDEs, but both of them are as good as the other.
As far as I know, there is no (considerable) difference in performance.
You could also consider using a template engine, however, that does impact performance. The most popular template engine is Smarty, but there are others (some better, some worse) out there.
I have blocks of HTML code in a MySQL database and my framework needs to print these within a PHP template which will be outputted to the browser. To do so I make this call:
</tr>
<!-- Section 3 -->
<?php echo SIN_SiteView::get('section3') ?>
<tr>
Which gets the code either from the APC or MySQL, now the code it obtains looks like this:
<td height="280" colspan="2" bgcolor="#00abd2">
<a href="#">
<img src="<?php echo SIN_Utilities::l("image", "home_flash.png")?>" width="710" height="280" border="0" />
</a>
As you can see I need to run all images through a method known as "l" which I use to easily change images paths. Now the issue is if I echo that block of code it will simply be echoed as a string and not work.
I tried surrounding the php with '. [code] .' and removing the php but that also did not work. Does anyone have any ideas on how I could properly echo this to the page.
Thanks.
UPDATE: I think I need to be using the eval() command thanks to some of the comments, I simply do not understand how to implement it in my situation. Any simple examples would be greatly appreciated, for example how do I change this line:
<?php echo SIN_SiteView::get('section3') ?>
To echo the entire block featured above, thanks again.
I think you want eval rather than echo. See this slightly different question.
My solution would be to eval '?>'.$myhtml.'<?php'.
Is the marketing team adding the php code to the html you are storing?
If not, maybe you could change your <?php echo FUNCTION() ?> into #FUNCTION() and evolve your SIN_SiteView::get() into your own templating interpreter?
I agree with cHao though; it would probably be easier to adopt one of the templating packages out there and convert your data over.
You'll need to use eval to evaluate the inline PHP. However, this is potentially quite risky (eval is evil, etc.), especially if any of the content that's being fetched is user sourced.
e.g.: At the very least, what's the stop the user inlining...
<?php die(); ?>
...within the content they enter.
As such, you'll need to take a great deal of care, if there's really no alternative to this approach.
Some updates:
If you're new to PHP I'd recommend having a re-think. Chances are there's no need to use eval. (Unless there's a dynamically customised content on a per-user basis then you don't need it.) What are you trying to achieve?
What specific error/problem are you having? (I presume you're using var_dump or print_r for debug purposes, etc.) As the content you need to eval isn't pure PHP (it's HTML with PHP in) you'll need to embed the PHP close and (re-)open tags as #Borealid illustrated.