vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Little questions on PHP string syntax. (https://vborg.vbsupport.ru/showthread.php?t=170111)

MrApples 02-10-2008 05:19 AM

Little questions on PHP string syntax.
 
$var = "<img width="5" src="/blah.png">"; is bad?
$var = '<img width="5" src="/blah.png">'; is good?
$var = "<img width=\"5\" src=\"/blah.png\">"; is good?
$var = '<img width=\"5\" src=\"/blah.png\">'; is bad?
$var = "<img width='5' src='/blah.png'>"; is good?
$var = "<img width=\'5\' src=\'/blah.png\'>"; is bad?
$var = "start " . $var . " end"; is good?

If someone could clarify this that would be great. Yes they are all different.

Marco van Herwaarden 02-10-2008 08:00 AM

Simple. Inside HTML text (ie. src="..") double quotes are used to identify text.

With PHP you have 2 options to enclose something:
- Single quotes: No evaluation of the content takes place
- Double quotes: Content is evaluated (ie. variables and such)

If you use double quotes inside a text, and you enclose the text with double quotes, then PHP will not know where the text starts and where it ends. It will read the first double quote as start en the 2nd (ie. the one embedded in the text) as closing quote. To avoid this enclose text that has a double-quote inside by single quotes. Alternative would be to escape special characters like the doube-quote with the escape character \.

Opserty 02-10-2008 08:32 AM

My personal preference is single quotes then escaping for variables:
PHP Code:

$string '<img src="someimage.gif" alt="'$alt_text .'" />'

Official documentation: http://uk3.php.net/manual/en/language.types.string.php if you want to read it.

MiahBeSmokin420 02-10-2008 11:52 AM

dream weaver man dream weaver thats all i can really say

dream weaver basically builds stuff for you a you go to do stuff

MrApples 02-10-2008 05:23 PM

Alright, I got it, thanks for your help. I'm almost sure that the questions I originally posted are all right (is good, is bad)

PHP only evaluates " " is good to know, thanks.


All times are GMT. The time now is 08:52 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02542 seconds
  • Memory Usage 1,717KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete